EACCES:尝试在节点项目中使用Docker卷时,权限被拒绝mkdir ...

问题描述:

我试图通过-> p

docker run -p 3000:3000 -v /myapp/node_modules -v $(pwd):/myapp batzu/frontend 

出现错误-

EACCES: permission denied, mkdir '/myapp/node_modules/.cache'

但是当我尝试运行不带-v标志的相同容器时-

But when I try to run the same container without the -v flags -

docker run -p 3000:3000 batzu/frontend

容器启动正常,没有错误.

The container starts just fine without error.

第一个docker运行日志-

First docker run logs-

> frontend@0.1.0 start
> react-scripts start

ℹ 「wds」: Project is running at http://172.17.0.2/
ℹ 「wds」: webpack output is served from 
ℹ 「wds」: Content not from webpack is served from /myapp/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...

Failed to compile.

EACCES: permission denied, mkdir '/myapp/node_modules/.cache'

使用的构建命令(该应用是react-app示例)-

Build command used (the app is an example react-app)-

docker build -f Dockerfile.dev -t batzu/frontend .

我的Dockerfile.dev-

My Dockerfile.dev -

FROM node:alpine

WORKDIR /myapp

COPY package.json .
RUN npm install
COPY . .

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

react项目生成的

package.json 文件-

package.json file generated by the react project-

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.5",
    "@testing-library/user-event": "^12.7.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.2",
    "web-vitals": "^1.1.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

我试图将 sh 放入容器并从中执行 npm run start ,但是即使我能够执行 mkdir'/myapp,也遇到了相同的错误/node_modules/.cache',没有任何权限错误.我无法理解docker中的 -v/myapp/node_modules 到底运行了什么,正如我被告知应该在 node_modules 文件夹之前添加书签的功能一样我们将工作目录映射到容器的/myapp/目录.为什么这会导致错误?

I tried to sh into the container and execute npm run start from it but got the same error even though I am able to execute mkdir '/myapp/node_modules/.cache' from the shell without any permission error. I am not able to understand what does the -v /myapp/node_modules in the docker run exactly does as I was told it is supposed to sort of bookmark the node_modules folder before we map our working directory to the /myapp/ dir of the container. Why does this cause an error??

有人可以为我指出正确的方向或解释我做错了什么还是解决....

Can someone point me the right direction or explain what am I doing wrong or a fix....?

非常感谢:)

所以,从我的视线中看到的其他人的Dockerfile和容器中bash终端中的一些运行命令,我得到的是我用作基础的node:alpine 图像在容器内创建了一个名为 node 的用户.

So, what I gathered from looking around and seeing other people's Dockerfiles and a bit of running commands in a bash terminal in my container, is that the node:alpine image I used as base, creates a user named node inside the container.

npm运行启动是作为没有root特权的 node 用户(具有node的 UID 的进程)执行的.(这是我在 htop 中查找时发现的),因此为了使 node 用户成为/app目录的所有者,我将其添加到了docker文件中-

The npm run start is executed as a process of node user(process with the UID of node) which does not have root privileges (which I found out while looking in the htop), so in order to make node user the owner of /app directory I added this in the docker file-

RUN chown -R node.node /app

更新后的Dockerfile.dev是-

The updated Dockerfile.dev is-

FROM node:alpine

WORKDIR '/app'

COPY package.json .
RUN npm install
RUN chown -R node.node /app

COPY . .

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

这已为我解决了问题,希望它也可以帮助其他人.:)

This has fixed the problem for me, hope it can help someone else too. :)