无法在运行docker-compose时找到services package.json。

huangapple go评论70阅读模式
英文:

Can't find services package.json when running docker-compose

问题

我正在在一个工作空间内构建一个 docker-compose.yml 文件,但是当我尝试运行 docker-compose 时,由于以下错误 ENOENT: no such file or directory, open '/entity-service/package.json',我的服务无法启动,同时,在尝试启动 agent-portal-service 容器时也出现了相同的错误,但是错误发生在这个目录中 /agent-portal-service/package.json。但我的项目确实有一个 package.json 文件,我认为这与 docker-compose 运行的上下文有关。

这是我的工作空间结构:

├── agent-portal-service
├── data
├── docker-compose.yml
└── 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-workspace:/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:
      - /agent-workspace:/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_serviceentity_service 容器。

英文:

I am building a docker-compose.yml file inside a workspace, but when I try to run docker-compose, I can't start my services because of the following error ENOENT: no such file or directory, open '/entity-service/package.json' the same error happens when trying to start the agent-portal-service container as well but the error happens in this directory /agent-portal-service/package.json. But my projects indeed have a package.json file, I think that this is related to the context that docker-compose is running.

Here is my workspace structure:

├── agent-portal-service
├── data
├── docker-compose.yml
└── entity-service

My docker-compose.yml:

version: "3"

services:
  agent_portal_service:
    working_dir: /agent-portal-service
    container_name: agent_portal_service
    image: node:16
    volumes:
      - /agent-workspace:/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:
      - /agent-workspace:/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

I am expecting to be able to run the agent_portal_service and entity_service containers successfully

答案1

得分: 1

您实体服务定义的重要部分如下所示:

entity_service:
  working_dir: /entity-service
  volumes:
    - /agent-workspace:/entity-service
  command: npm run start:debug

您在卷部分将 /agent-workspace 映射到 /entity-service。您还将工作目录设置为 /entity-service。实际上,您的工作目录是在主机机器上的 /agent-workspace/agent-workspace 目录中没有 package.json 文件,因此您的 npm 命令失败。

为了解决这个问题,我建议将 /agent-workspace/entity-service 映射到 /entity-service,这样您容器内的 /entity-service 目录将包含一个 package.json 文件。

像这样:

volumes:
  - /agent-workspace/entity-service:/entity-service
英文:

The important parts of your entity-service definition look like this

  entity_service:
    working_dir: /entity-service
    volumes:
      - /agent-workspace:/entity-service
    command: npm run start:debug

You map /agent-workspace to /entity-service in the volumes section. You also set your working directory to /entity-service. In effect, your working directory is /agent-workspace on your host machine. There's no package.json file in the /agent-workspace directory, so your npm command fails.

To fix it, I'd map /agent-workspace/entity-service to /entity-service instead. That way, you'll have a package.json file in your /entity-service directory inside the container.

Like this

volumes:
  - /agent-workspace/entity-service:/entity-service

huangapple
  • 本文由 发表于 2023年6月2日 02:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定