Docker-“错误:EACCES:权限被拒绝,mkdir'/project/node_modules/.cache/@babel'”

时间:2019-04-30 18:20:33

标签: node.js docker docker-compose yarnpkg

运行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

我的仓库位于https://github.com/cozy-nyc/cozy-nyc

5 个答案:

答案 0 :(得分:7)

我对npm有同样的问题。我使用:

RUN npm config set unsafe-perm true

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

npm install -g --unsafe-perm thePackage

您可以在此处找到其文档:https://docs.npmjs.com/misc/config#unsafe-perm

答案 1 :(得分:3)

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

使用this SO answer查找卷映射到的本地目录,并使用以下方式更改所有者:

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

答案 2 :(得分:0)

尝试在Dockerfile中运行RUN --mount=type=ssh,id=github_ssh_key pip install private-repo && more commands ...之前设置用户。

yarn

答案 3 :(得分:0)

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

类似的东西:

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

答案 4 :(得分:0)

我建议像这里建议的那样做 (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