docker 容器中的 NPM 安装失败并且 returns "The command '/bin/sh -c npm install' returned a non-zero code: 1"

NPM install in docker container fails and returns "The command '/bin/sh -c npm install' returned a non-zero code: 1"

我正在尝试使用 gitlab CI/CD 将我的 dockerized MERN 堆栈 Web 应用程序部署到我的 vps。在我的 React 应用程序 dockerfile 中,当我尝试安装 npm 包时,出现此错误:

服务 'nowfront' 构建失败:命令“/bin/sh -c npm install”返回非零代码:1

这是我的 Dockerfile:

# pull official base image
FROM node as builder

# make directory
RUN mkdir -p /app
RUN chmod -R 777 /app

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies
COPY package.json .
RUN npm install

# add app
COPY . .
COPY .env .
RUN ls -li
#RUN yarn build
#CMD ["npm","run","build"]
RUN ls

FROM nginx:stable-alpine
COPY --from=builder /app/build /usr/share/nginx/html 
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

我的 Ubuntu 版本是 20.04.1 LTS。

有解决办法吗?

我更新了我的 Dockerfile 并添加了第 14 行,它现在可以工作了。

# pull official base image
FROM node as builder

# make directory
RUN mkdir -p /app
RUN chmod -R 777 /app

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies
COPY package.json .
COPY package-lock.json .
RUN npm install

# add app
COPY . .
COPY .env .
RUN ls -li
#RUN yarn build
#CMD ["npm","run","build"]
RUN ls

FROM nginx:stable-alpine
COPY --from=builder /app/build /usr/share/nginx/html 
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]