无法npm安装在节点docker映像中

时间:2019-10-02 05:36:50

标签: docker docker-compose

FROM node:latest

WORKDIR /frontend/

ENV PATH /frontend/node_modules/.bin:$PATH

COPY package.json /frontend/package.json
COPY . /frontend/
RUN npm install --silent
RUN npm install react-scripts@3.0.1 -g --silent

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

这是我的项目Dockerfile的前端。

我将此作为服务之一放在我的docker-compose.yml文件中,当我运行docker-compose up -d --build时,它给了我

Step 6/8 : RUN npm install --silent
 ---> Running in 09a4f59a96fa
ERROR: Service 'frontend' failed to build: The command '/bin/sh -c npm install --silent' returned a non-zero code: 1

我的docker-compose文件如下所示:

# Docker Compose
version: '3.7'

services:
  frontend:
    container_name: frontend
    build:
      context: frontend
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    volumes:
      - '.:/frontend'
      - '/frontend/node_modules'
  backend:
    build: ./backend
    ports:
      - "5000:5000"
    volumes:
       - .:/code

预先感谢

编辑:构建后前端出错

enter image description here

1 个答案:

答案 0 :(得分:0)

对于docker-compose,我认为应该像

- ./frontend/:/frontend'

因为构建上下文是前端。

第二,如果您使用的是卷,那为什么要在Dockerfile中安装和复制代码呢?如果您正在使用绑定卷,则将其从Dockerfile中删除,因为它们将从主机代码中被覆盖。

COPY package.json /frontend/package.json
COPY . /frontend/
相关问题