Docker容器中的权限被拒绝

Docker容器中的权限被拒绝,docker,dockerfile,Docker,Dockerfile,我是Docker新手,我正在尝试按照教程运行容器,在使用-volume或-v时遇到了一些问题,如下所示: docker run -v <absolute path to host directory>:<absolute path to container working directory> <image id> 引发的错误如下: npm ERR! code EACCES npm ERR! syscall open npm ERR! path /usr/a

我是Docker新手,我正在尝试按照教程运行容器,在使用-volume或-v时遇到了一些问题,如下所示:

docker run -v <absolute path to host directory>:<absolute path to container working directory> <image id>
引发的错误如下:

npm ERR! code EACCES
npm ERR! syscall open
npm ERR! path /usr/app/package.json
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, open '/usr/app/package.json'
npm ERR!  [Error: EACCES: permission denied, open '/usr/app/package.json'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'open',
npm ERR!   path: '/usr/app/package.json'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-11-12T13_39_38_626Z-debug.log
我已经建立了没有任何问题的形象:

docker build -f Dockerfile.dev .
Sending build context to Docker daemon  23.55kB
Step 1/6 : FROM node
 ---> f1974cfde44f
Step 2/6 : WORKDIR /usr/app
 ---> Using cache
 ---> 120da5dd5fa4
Step 3/6 : COPY ./package.json ./
 ---> Using cache
 ---> 040ba41014c8
Step 4/6 : RUN npm install
 ---> Using cache
 ---> 96315e12e5d0
Step 5/6 : COPY . .
 ---> db93b8725037
Step 6/6 : CMD [ "npm", "run", "dev" ]
 ---> Running in 9c9d58318420
Removing intermediate container 9c9d58318420
 ---> 900a3497fc39
Successfully built 900a3497fc39
我的Dockerfile.dev如下所示:

docker run -p 3000:3000 -v /usr/app/node_modules -v "$(pwd)":/usr/app 900a3497fc39
FROM node

WORKDIR /usr/app

COPY ./package.json ./
RUN npm install

COPY . .

CMD [ "npm", "run", "dev" ]
{
  "name": "vite-counter",
  "scripts": {
    "dev": "vite",
    "build": "vite build"
  },
  "dependencies": {
    "vue": "^3.0.0-beta.15"
  },
  "devDependencies": {
    "vite": "^1.0.0-beta.3",
    "@vue/compiler-sfc": "^3.0.0-beta.15"
  }
}
package.json如下所示:

docker run -p 3000:3000 -v /usr/app/node_modules -v "$(pwd)":/usr/app 900a3497fc39
FROM node

WORKDIR /usr/app

COPY ./package.json ./
RUN npm install

COPY . .

CMD [ "npm", "run", "dev" ]
{
  "name": "vite-counter",
  "scripts": {
    "dev": "vite",
    "build": "vite build"
  },
  "dependencies": {
    "vue": "^3.0.0-beta.15"
  },
  "devDependencies": {
    "vite": "^1.0.0-beta.3",
    "@vue/compiler-sfc": "^3.0.0-beta.15"
  }
}

据我所知,我在容器中没有足够的权限运行此操作。这正是我在教程中遵循的内容。我做错了什么以及如何修复此问题?

代表节点用户使用COPY查看此处的答案:,COPY-chown=node:node package.json。运行npm安装副本-chown=node:node。或者看看这里的答案