即使安装了卷,使用Nodemon和Docker也会出现“错误:找不到模块”

时间:2019-02-12 10:05:32

标签: docker docker-compose

在Docker内部运行nodemon并保存节点文件时,我不断收到错误消息,指出模块不存在。抛出错误之前,需要进行两次保存。我已经按照答案here的建议安装了卷,但是仍然出现错误,我不太确定是什么原因引起的。

这是我的docker-compose.yml文件。

version: "3.7"
services:
  api:
    container_name: api
    build:
      context: ./api
      target: development
    restart: on-failure
    ports:
      - "3000:3000"
      - "9229:9229"
    volumes:
      - "./api:/home/node/app"
      - "node_modules:/home/node/app/node_modules"
    depends_on:
      - db
    networks:
      - backend
  db:
    container_name: db
    command: mongod --noauth --smallfiles
    image: mongo
    restart: on-failure
    volumes:
      - "mongo-data:/data/db"
      - "./scripts:/scripts"
      - "./data:/data/"
    ports:
      - "27017:27017"
    networks: 
      - backend
networks:
  backend:
    driver: bridge
volumes:
  mongo-data:
  node_modules:

这是我的docker文件:

# Ger current Node Alpine Linux image.
FROM node:alpine AS base
# Expose port 3000 for node.
EXPOSE 3000
# Set working directory.
WORKDIR /home/node/app
# Copy project content.
COPY package*.json ./

# Development environment.
FROM base AS development
# Set environment of node to development to trigger flag.
ENV NODE_ENV=development
# Express flag.
ENV DEBUG=app
# Run NPM install.
RUN npm install
# Copy source code.
COPY . /home/node/app
# Run the app.
CMD [ "npm", "start" ]

# Production environment.
FROM base AS production
# Set environment of node to production to trigger flag.
ENV NODE_ENV=production
# Run NPM install.
RUN npm install --only=production --no-optional && npm cache clean --force
# Copy source code.
COPY . /home/node/app
# Set user to node for better security.
USER node
# Run the app.
CMD [ "npm", "run", "start:prod" ]

1 个答案:

答案 0 :(得分:0)

结果是我没有将.dockerignore放在正确的文件夹中。您应该将其放在上下文文件夹中。