无法通过 Docker 个容器 运行 NodeJS 微服务

Unable to run NodeJS microservices through Docker containers

我开始使用 Docker,创建了几个微型 Express(NodeJS) 服务。 计划是 运行 Docker 容器内的微服务,然后使用 Docker 组合服务名称在它们之间建立相互通信。

这是这个简单项目的Github repo。能够使用以下命令构建图像:

cd books
docker build -t node-micro/books .
cd auth
docker build -t node-micro/auth .

启动容器的命令:

docker run -d -p 6677:6677 node-micro/auth

docker run -d -p 7766:7766 node-micro/books

但是当我点击 URL 以下时,没有任何反应,前几天工作正常:

http://localhost:6677/

http://localhost:7766/

并且不知道 docker compose 发生了什么。停止所有容器后无法访问与上述相同的 URL,请删除所有图像和 运行 此命令:

docker-compose up -d

在单独启动容器以及通过 docker-compose 启动容器方面需要一些帮助。

您正在从两个 Dockerfile 中公开端口 3000。替换 docker-compose.yml 文件中每个微服务的端口。

  - "7766:3000"
  - "6677:3000"

端口映射错误。在两个 Dockerfile 中,您都公开了端口 3000。 所以你必须将端口 6677 和 7766 映射到 Dockerfile 上的暴露端口。

要解决此问题,在您的 docker-compose.yml 上,您必须像这样配置端口:

version: '3'
services:
  books:
    build: './books'
    ports:
      - "7766:3000"
    depends_on: 
      - auth

  auth:
    build: './auth'
    ports:
      - "6677:3000"

我可以在您的每个微服务中看到,您的应用程序 运行 正在容器中的端口 3000 上运行,但您在 docker-compose.yml

请检查以下内容docker-compose.yml

version: '3'
services:
  books:
    build: './books'
    ports:
      - "7766:3000"
    depends_on: 
      - auth

  auth:
    build: './auth'
    ports:
      - "6677:3005"

然后运行下面的命令

docker-compose up --build 

--build 也将构建图像。

然后,您应该可以访问该服务

http://localhost:6677/

http://localhost:7766/

输出

docker-compose up --build
Creating network "node_microservices_default" with the default driver
Building auth
Step 1/7 : FROM node:10-alpine
 ---> 0aa7bb41deca
Step 2/7 : WORKDIR /usr
 ---> Running in a1dc67b70538
Removing intermediate container a1dc67b70538
 ---> 5fc74fc80a14
Step 3/7 : COPY package*.json ./
 ---> 454f1b7aba87
Step 4/7 : RUN npm install
 ---> Running in a24eea8b79d4
npm WARN auth@1.0.0 No description
npm WARN auth@1.0.0 No repository field.

added 50 packages from 37 contributors and audited 50 packages in 8.58s
found 0 vulnerabilities

Removing intermediate container a24eea8b79d4
 ---> 31b31ff4516e
Step 5/7 : COPY . .
 ---> 1eeaa8e70300
Step 6/7 : EXPOSE 3000
 ---> Running in fc798167dbcd
Removing intermediate container fc798167dbcd
 ---> 4d964d25c099
Step 7/7 : CMD ["npm", "start"]
 ---> Running in 3c28d92f9ef6
Removing intermediate container 3c28d92f9ef6
 ---> 514f68d11d7c
Successfully built 514f68d11d7c
Successfully tagged node_microservices_auth:latest
Building books
Step 1/7 : FROM node:10-alpine
 ---> 0aa7bb41deca
Step 2/7 : WORKDIR /usr
 ---> Using cache
 ---> 5fc74fc80a14
Step 3/7 : COPY package*.json ./
 ---> 56addb6c75a5
Step 4/7 : RUN npm install
 ---> Running in 4864fb7a171c
npm WARN books@1.0.0 No description
npm WARN books@1.0.0 No repository field.

added 50 packages from 37 contributors and audited 50 packages in 5.111s
found 0 vulnerabilities

Removing intermediate container 4864fb7a171c
 ---> 82bb2cd54357
Step 5/7 : COPY . .
 ---> 12893a93e82e
Step 6/7 : EXPOSE 3000
 ---> Running in 1301e29dbd52
Removing intermediate container 1301e29dbd52
 ---> c26948ebcb3b
Step 7/7 : CMD ["npm", "start"]
 ---> Running in db948866a121
Removing intermediate container db948866a121
 ---> 703b901d7bc4
Successfully built 703b901d7bc4
Successfully tagged node_microservices_books:latest
Creating node_microservices_auth_1 ... done
Creating node_microservices_books_1 ... done
Attaching to node_microservices_auth_1, node_microservices_books_1
auth_1   | 
auth_1   | > auth@1.0.0 start /usr
auth_1   | > node index.js
auth_1   | 
auth_1   | Running on port 3005
auth_1   | --------------------------
books_1  | 
books_1  | > books@1.0.0 start /usr
books_1  | > node index.js
books_1  | 
books_1  | Running on port 3000
books_1  | --------------------------