Docker - EACCES:权限被拒绝,mkdir '/app/node_modules/.cache'

时间:2021-04-21 00:10:00

标签: linux docker permissions

我今天刚开始使用 docker,但由于权限问题而被阻止。我不知道我应该输入什么来切换权限。我假设这是一个 chown 的事情。还有其他关于堆栈溢出的问题,但它们对我没有帮助,因为它们要么不是特定于 Docker 的,要么没有针对该问题的选定答案。

下面是错误

client_1  | 
client_1  | > client@0.1.0 start
client_1  | > react-scripts start
client_1  | 
client_1  | ℹ 「wds」: Project is running at http://172.19.0.2/
client_1  | ℹ 「wds」: webpack output is served from 
client_1  | ℹ 「wds」: Content not from webpack is served from /app/public
client_1  | ℹ 「wds」: 404s will fallback to /
client_1  | Starting the development server...
client_1  | 
client_1  | Failed to compile.
client_1  | 
client_1  | EACCES: permission denied, mkdir '/app/node_modules/.cache'

我的 docker-compose.yml 看起来像这样:

version: "3"
services:
  client:
    stdin_open: true
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "3000:3000"
    volumes:
      - "/app/node_modules"
      - "./:/app"

我的 Dockerfile.dev 如下所示:

FROM node:alpine
RUN apk update && apk add git && apk add python make g++

WORKDIR /app

COPY package.json /app

# To Fix Permissions for Packages
RUN npm config set unsafe-perm true

RUN npm install --force

COPY . /app

CMD ["npm", "run", "start"]

2 个答案:

答案 0 :(得分:3)

我曾经遇到过类似的问题。我的解释是,当应用程序运行时,它不是以 root 身份运行的,因此当它尝试创建 .cache 目录时,它不再具有正确的凭据来执行此操作。我使用了一个非常相似的图像 (node:lts-alpine),它已经带有 node 用户。我所做的,但我不确定这是否符合最佳实践,正在创建一个名为 .cache 的文件,它应该被找到,更改其所有权,以便节点可以访问它并将应用程序作为 {{ 1}}。我实际上以 node 的身份运行应用程序,因为这是我部署到 heroku 的,并且容器不是以 root 身份运行的。在您的情况下,它看起来像:

node

答案 1 :(得分:0)

我遇到了同样的错误,把 docker 文件放在下面解决了它。

RUN npm config set unsafe-perm true

RUN npm install --force

COPY ./ /usr/app/

RUN chown -R node /usr/app/node_modules

USER node
相关问题