简体   繁体   中英

Unable to run NodeJS microservices through Docker containers

I was getting started with Docker, created couple of tiny Express(NodeJS) services. Plan is to run the microservices inside Docker containers and then establish a inter-communication between them using Docker Compose service names.

Here is the Github repo of this simple project. Am able to build images with below commands:

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

Commands to start containers:

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

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

But when i hit below URL's there is no response, which was working fine couple of day's before:

http://localhost:6677/

http://localhost:7766/

And have no clue what's happening with docker compose. No luck on accessing same URL's as mentioned above after stoping all containers, delete all images & ran this command:

docker-compose up -d

Need some help on bringing up the containers individually and also through docker-compose.

I can see in each of your micro-service, your application is running on ports 3000 in the container but you are exposing 7766 and 6677 in your docker-compose.yml

Please check the below docker-compose.yml

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

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

and then run the below command

docker-compose up --build 

--build will build the images as well.

Then, you should be able to access the service

http://localhost:6677/

http://localhost:7766/

Output

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  | --------------------------

You are exposing from both Dockerfiles the port 3000. Replace the port for each microservice in docker-compose.yml file.

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

The mapping for ports is wrong. In both Dockerfile you are exposing the port 3000. So you must to map the ports 6677 and 7766 to the exposed port on the Dockerfile.

To fix this, on your docker-compose.yml you must to config ports like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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