简体   繁体   English

如果本地计算机上未安装 node_modules,则 Docker 组合不起作用

[英]Docker compose does not work if node_modules are not installed on local machine

My goal is to create docker dev environment for a full-stack app: React, NodeJS and MongoDb (with hot reloading).我的目标是为全栈应用程序创建 docker 开发环境:React、NodeJS 和 MongoDb(热重载)。 My current dev environment works, but I noticed that "docker compose up" will only work, if the node_modules are installed on my local machine - it otherwise returns an error that nodemon is not installed, or react-scripts not found.我当前的开发环境可以工作,但我注意到“docker compose up”只有在我的本地机器上安装了 node_modules 时才能工作 - 否则它会返回一个错误,即 nodemon 未安装或 react-scripts 未找到。 It seems like docker is looking for the node files in my local machine, but it should be installing them in the container during the compose, right?似乎 docker 正在我的本地计算机中查找节点文件,但它应该在撰写期间将它们安装在容器中,对吧?

docker-compose.yml docker-compose.yml

version: '3.8'
services:
  server:
    build: ./server
    container_name: server_backend
    ports:
      - '7000:7000'
    volumes:
      - ./server:/app
  client:
    build: ./client
    container_name: client_frontend
    ports:
      - '3000:3000'
    volumes:
      - ./client:/app
    stdin_open: true
    tty: true

Dockerfile (backend server) Dockerfile(后端服务器)

FROM node:16-bullseye-slim
WORKDIR /app
COPY package.json .
COPY package-lock.json .
RUN npm install
COPY . .
EXPOSE 7000
CMD ["node", "index.js"]

Dockerfile (frontend) Dockerfile(前端)

FROM node:16-bullseye-slim
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

app architecture应用架构

- MyApp
-- client
-- server

Can it be that you are copying the whole directory with the node_modules when you execute the COPY. .执行COPY. .时,您是否正在使用node_modules复制整个目录? COPY. . command, try and either specifiy the directory to copy or add a .dockerignore .命令,尝试指定要复制的目录或添加.dockerignore

Found this in the documentation, https://docs.docker.com/language/nodejs/build-images/#create-a-dockerignore-file在文档中找到了这个, https://docs.docker.com/language/nodejs/build-images/#create-a-dockerignore-file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM