嗨,我目前正在学习Docker,我想知道为什么我的代码确实返回了一个错误,虽然它已经解决了,但我只是怀疑为什么我会面临这个问题。
下面是我的docker代码:
FROM node:16.20.0-alpine3.17
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /usr/app
COPY package*.json /usr/app
RUN npm install
COPY . .
ENV API_URL=http://api.myapp.com/
EXPOSE 3000
CMD npm run
以下是我在构建阶段遇到的问题:
#0 22.90 npm notice
#0 22.90 npm notice New major version of npm available! 8.19.4 -> 9.6.4
#0 22.90 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v9.6.4>
#0 22.90 npm notice Run `npm install -g npm@9.6.4` to update!
#0 22.90 npm notice
#0 22.90 npm ERR! code EACCES
#0 22.90 npm ERR! syscall open
#0 22.90 npm ERR! path /usr/app/package-lock.json
#0 22.90 npm ERR! errno -13
#0 22.90 npm ERR! Error: EACCES: permission denied, open '/usr/app/package-lock.json'
#0 22.90 npm ERR! [Error: EACCES: permission denied, open '/usr/app/package-lock.json'] {
#0 22.90 npm ERR! errno: -13,
#0 22.90 npm ERR! code: 'EACCES',
#0 22.90 npm ERR! syscall: 'open',
#0 22.90 npm ERR! path: '/usr/app/package-lock.json'
#0 22.90 npm ERR! }
#0 22.90 npm ERR!
#0 22.90 npm ERR! The operation was rejected by your operating system.
#0 22.90 npm ERR! It is likely you do not have the permissions to access this file as the current user
#0 22.90 npm ERR!
#0 22.90 npm ERR! If you believe this might be a permissions issue, please double-check the
#0 22.90 npm ERR! permissions of the file and its containing directories, or try running
#0 22.90 npm ERR! the command again as root/Administrator.
#0 22.90
#0 22.90 npm ERR! A complete log of this run can be found in:
#0 22.90 npm ERR! /home/app/.npm/_logs/2023-04-07T10_17_38_968Z-debug-0.log
------
Dockerfile:6
--------------------
4 | WORKDIR /usr/app
5 | COPY package*.json /usr/app
6 | >>> RUN npm install
7 | COPY . .
8 | ENV API_URL=http://api.myapp.com/
--------------------
ERROR: failed to solve: process "/bin/sh -c npm install" did not complete successfully: exit code: 243
顺便说一句,只是为了大家的参考。这是我得到的修复:
FROM node:14.16.0-alpine3.13
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
ENV API_URL=http://api.myapp.com/
EXPOSE 3000
CMD ["npm", "start"]
提前感谢各位的回答!
我期待一个解释,为什么我以前的Docker代码遇到了一些问题
1条答案
按热度按时间oxiaedzo1#
将
USER app
行移动到文件的末尾。WORKDIR
和COPY
通常以root用户的身份创建目录和文件,即使当前用户是不同的。在序列中,当你RUN npm install
时,你是app
用户。这想要重写package-lock.json
文件,但前一行COPY
将其编辑为root
所有,这就是你看到的权限错误。以root用户身份运行镜像构建应该不会有什么害处。完成构建后,
node_modules
树和应用程序的其余部分将归root
所有,但它们是全世界可读的,因此以app
身份运行仍然可以正常工作。这将为您提供一点保护,防止意外覆盖应用程序的代码、库或静态资产。