【问题标题】:Docker - "Error: EACCES: permission denied, mkdir '/project/node_modules/.cache/@babel'"Docker - “错误:EACCES:权限被拒绝,mkdir '/project/node_modules/.cache/@babel'”
【发布时间】:2019-09-19 10:56:43
【问题描述】:

运行yarn docker-build 工作正常,但当yarn docker-upyarn docker-dev 调用RUN yarn 时会弹出错误。 Nginx 启动正常,但 yarn 无法进入 Projects 目录中的 mkdir。

package.json

...
    "docker-build": "docker-compose build",
    "docker-dev": "cross-env NGINX_HOST=localhost NGINX_PORT=3000 PORT=3000 docker-compose -f docker-compose.yml -f docker-compose.dev.yml up --no-deps",
    "docker-up": "cross-env NGINX_HOST=localhost NGINX_PORT=80 PORT=8080 docker-compose -f docker-compose.yml -f docker-compose.prod.yml up --no-deps -d",
    "docker-down": "docker-compose down"
...

Dockerfile

FROM mhart/alpine-node:8

# Install required dependencies (Alpine Linux packages)
RUN apk update && \
  apk add --no-cache \
    sudo \
    g++ \
    gcc \
    git \
    libev-dev \
    libevent-dev \
    libuv-dev \
    make \
    openssl-dev \
    perl \
    python

# Add user and make it sudoer
ARG uid=1000
ARG user=username
RUN set -x ; \
  addgroup -g $uid -S $user ; \
  adduser -u $uid -D -S -G $user $user \
  && exit 0 ; exit 1
RUN echo $user' ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# Install (global) Yarn packages/dependencies
RUN yarn global add node-gyp
RUN git clone --recursive https://github.com/sass/node-sass.git \
  && cd node-sass \
  && yarn \
  && node scripts/build -f

# Make project directory with permissions
RUN mkdir /project

# Switch to project directory
WORKDIR /project

# Copy required stuff
COPY . .

# Give owner rights to the current user
RUN chown -Rh $user:$user /project

# Install (local) Yarn packages and build
RUN yarn

USER $user

错误

app_1    | [2] Error: EACCES: permission denied, mkdir '/project/node_modules/.cache/@babel'
app_1    | [2]     at Object.fs.mkdirSync (fs.js:885:18)
app_1    | [2]     at sync (/project/node_modules/mkdirp/index.js:71:13)
app_1    | [2]     at sync (/project/node_modules/mkdirp/index.js:77:24)
app_1    | [2]     at save (/project/node_modules/@babel/register/lib/cache.js:50:20)
app_1    | [2]     at _combinedTickCallback (internal/process/next_tick.js:132:7)
app_1    | [2]     at process._tickCallback (internal/process/next_tick.js:181:9)
app_1    | [2]     at Function.Module.runMain (module.js:696:11)
app_1    | [2]     at startup (bootstrap_node.js:204:16)
app_1    | [2]     at bootstrap_node.js:625:3

我的 repo 可以在这里找到https://github.com/cozy-nyc/cozy-nyc

【问题讨论】:

  • ARG user=username中的用户名从何而来?

标签: node.js docker docker-compose yarnpkg


【解决方案1】:

在您的 Dockerfile 中运行 yarn 之前尝试设置用户。

# Give owner rights to the current user
RUN chown -Rh $user:$user /project

USER $user

# Install (local) Yarn packages and build
RUN yarn

【讨论】:

    【解决方案2】:

    尝试将./node_modules 的卷添加到您的应用程序服务下的 docker-compose 文件中。

    类似:

    services:
      app:
        command: yarn start
        volumes:
          - .:/project
          - ./node_modules:/project/node_modules
    

    【讨论】:

      【解决方案3】:

      我在使用 npm 时遇到了同样的问题。我使用以下方法修复了它:

      RUN npm config set unsafe-perm true
      

      另一种方法是在你的安装命令中指定它:

      npm install -g --unsafe-perm thePackage
      

      你可以在这里找到它的文档:https://docs.npmjs.com/misc/config#unsafe-perm

      【讨论】:

        【解决方案4】:

        您可能在docker-compose 文件中指定了一个卷并安装为/project/node_modules,您的用户无权访问本地磁盘上的该目录。

        使用this SO answer 找出卷映射到哪个本地目录并更改所有者:

        sudo chown -R yourusername:yourusername /path/to/node_modules
        

        【讨论】:

          【解决方案5】:

          我建议按照这里的建议 (https://github.com/nodejs/docker-node/issues/1262#issuecomment-677577653)。所以从这里改变以下几行

          # Copy required stuff
          COPY . .
          
          # Give owner rights to the current user
          RUN chown -Rh $user:$user /project
          

          到这里

          # Copy required stuff
          COPY --chown $user:$user . .
          

          或者作为替代更改您的WORKDIR (https://github.com/nodejs/docker-node/issues/740#issuecomment-458545074):

          WORKDIR /home/node/project
          

          【讨论】:

            猜你喜欢
            • 2021-07-15
            • 1970-01-01
            • 2021-12-31
            • 2021-08-10
            • 2019-10-28
            • 1970-01-01
            • 2023-03-25
            • 2016-05-14
            • 2018-03-01
            相关资源
            最近更新 更多