1

我目前正在按照微服务架构使用NestJS构建应用程序。并使用NATS作为消息传递系统。它在我的本地机器上运行良好,但我无法对 NATS 服务器进行 docker 化。

我的项目的文件夹结构:

├─── services/
    ├─── nats/
    |       Dockerfile
    ├─── client/
    |       ...
    |       ...
    |       .dockerignore
    |       Dockerfile
    └─── docker-compose.yml

现在,Dockerfile内部nats/文件夹描述如下:

FROM nats:2.1.9

EXPOSE 4222

CMD [ "nats-server" ]

Dockerfile里面client/的文件夹描述为:

FROM node:12

WORKDIR /myProject/src/app

COPY package*.json ./

RUN npm install

COPY . .

RUN npm run build

EXPOSE 8081

CMD [ "node", "dist/main" ]

而且docker-compose.yml是:

version: '3'

services:
  nats-server:
    image: nats:2.1.9
    restart: always
    build: ./nats
    ports:
      - 4222:4222

  client-service:
    build: ./client
    volumes:
      - ./client:/myProject/src/app
    depends_on:
      - nats-server
    environment:
      NODE_ENV: development
    ports:
      - 8081:3000

我已经运行命令sudo docker-compose build并没有发现错误,然后运行命令sudo docker-compose up并发现以下错误:

Recreating services_nats-server_1 ... done
Recreating services_client-service_1 ... done
Attaching to services_nats-server_1, services_client-service_1
nats-server_1     | nats-server: unrecognized command: "nats-server_1"
services_nats-server_1 exited with code 1
nats-server_1     | nats-server: unrecognized command: "nats-server_1"
nats-server_1     | nats-server: unrecognized command: "nats-server_1"
nats-server_1     | nats-server: unrecognized command: "nats-server_1"
client-service_1  | [Nest] 1   - 12/03/2020, 4:44:34 PM   [NestFactory] Starting Nest application...
client-service_1  | [Nest] 1   - 12/03/2020, 4:44:34 PM   [InstanceLoader] AppModule dependencies initialized +41ms
client-service_1  | [Nest] 1   - 12/03/2020, 4:44:34 PM   [InstanceLoader] MongooseModule dependencies initialized +1ms
client-service_1  | [Nest] 1   - 12/03/2020, 4:44:34 PM   [InstanceLoader] ClientsModule dependencies initialized +1ms
client-service_1  | [Nest] 1   - 12/03/2020, 4:44:34 PM   [InstanceLoader] ClientsModule dependencies initialized +0ms
client-service_1  | [Nest] 1   - 12/03/2020, 4:44:34 PM   [InstanceLoader] ConfigHostModule dependencies initialized +1ms
client-service_1  | [Nest] 1   - 12/03/2020, 4:44:34 PM   [InstanceLoader] ConfigModule dependencies initialized +1ms
services_nats-server_1 exited with code 1
nats-server_1     | nats-server: unrecognized command: "nats-server_1"
nats-server_1     | nats-server: unrecognized command: "nats-server_1"
nats-server_1     | nats-server: unrecognized command: "nats-server_1"
nats-server_1     | nats-server: unrecognized command: "nats-server_1"
nats-server_1     | nats-server: unrecognized command: "nats-server_1"

客户端服务 Dockerfile 没有问题,因为它取决于 nats-server 尝试首先运行它。谁能帮我解决问题?

我要感谢您的时间和考虑。

4

2 回答 2

2

该服务的build:命令nats-server已覆盖 Docker Hub 映像。我只需要拉取图像并公开端口。

因此,我的docker-compose.yml文件将如下所示:

version: '3'

services:
  nats-server:
    image: nats:2.1.9
    restart: always
    ports:
      - 4222:4222

  client-service:
    build: ./client
    volumes:
      - ./client:/myProject/src/app
    depends_on:
      - nats-server
    environment:
      NODE_ENV: development
    ports:
      - 8081:3000
于 2020-12-04T09:48:45.780 回答
0

检查Dockerfile,您会注意到它ENTRYPOINT被设置为/nats-server并且CMDENTRYPOINT--config nats-server.conf在这种情况下)的参数。除非您扩展 nats 映像,否则您不需要基于它构建一个新映像(build: ./nats从 compose 文件中删除)。CMD可以通过设置来command:覆盖默认值docker-compose.yml

于 2020-12-04T00:26:47.180 回答