运行docker-compose时无法找到服务的package.json文件求助
解决Docker Compose启动Node服务时找不到package.json的问题
你的问题核心是卷挂载路径配置错误,导致容器内的服务目录没有正确映射到本地工作区的对应文件夹,所以找不到package.json。
问题分析
你当前的volumes配置用了绝对路径/agent-workspace,但这个路径和你本地工作区的结构不匹配。你的工作区根目录下有agent-portal-service和entity-service两个独立的服务目录,需要分别挂载到容器内对应的目录。
修正后的docker-compose.yml
version: "3" services: agent_portal_service: working_dir: /agent-portal-service container_name: agent_portal_service image: node:16 volumes: - ./agent-portal-service:/agent-portal-service # 修改为相对路径挂载本地服务目录 ports: - 3100:3100 command: npm run start:debug tty: true entity_service: working_dir: /entity-service container_name: entity_service image: node:16 volumes: - ./entity-service:/entity-service # 修改为相对路径挂载本地服务目录 ports: - 3101:3101 command: npm run start:debug tty: true depends_on: - mongodb mongodb: container_name: mongodb image: mongo:4.4.6 ports: - 27017:27017 volumes: - ./data/db:/data/db command: mongod --port 27017 restart: always
关键修改说明
- 对于
agent_portal_service,将volumes从/agent-workspace:/agent-portal-service改为./agent-portal-service:/agent-portal-service:./表示docker-compose.yml所在的工作区根目录,这样容器内的/agent-portal-service就会直接映射本地的agent-portal-service目录,里面的package.json自然能被找到。 - 同理,
entity_service的volumes修改为./entity-service:/entity-service,对应映射本地的entity-service目录。
修改后重新运行docker-compose up -d,两个Node服务应该就能正常启动了。
内容的提问来源于stack exchange,提问作者Davi Cheli Miquelim
相关产品推荐
相关产品推荐

