Search code examples
node.jsdockerdocker-composecontainersmicroservices

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.


Solution

  • 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 [email protected] No description
    npm WARN [email protected] 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 [email protected] No description
    npm WARN [email protected] 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   | > [email protected] start /usr
    auth_1   | > node index.js
    auth_1   | 
    auth_1   | Running on port 3005
    auth_1   | --------------------------
    books_1  | 
    books_1  | > [email protected] start /usr
    books_1  | > node index.js
    books_1  | 
    books_1  | Running on port 3000
    books_1  | --------------------------