Join us

Docker Composer with Nodejs App

1_QqBnMfkMKYgdZWlAyifSww (1).png

Recommendation :) Docker-compose built on top of the Docker container engine. To know more about Docker please follow this article which will give you a good sense to read this article.

What we will do in this article

  1. What is docker-compose? Why and when should we use it?
  2. Installation of docker-compose.
  3. Let's understand our services.
  4. Understand Docker-compose file.
  5. Run these services and access them through localhost.

Step 1: What is docker-compose? Why and when should we use it?

  • Docker compose is built on top of docker.
  • Docker compose solves the repeated work. Like build, deploy the docker files. Like in our above diagram we have three docker containers. So each container will have a build and run process.
  • It helps to reduce human effort. So, fewer errors occur while building and deploying the docker files

Step 2: Installation of docker-compose.

  • If you are using docker desktop to run your docker container then we do not require any extra installation steps.
  • If docker is used from docker CLI then need to follow the following steps.

Step 3: Let’s understand our services.

  • Clone this github repo.
  • Next shift to branch basic-docker-compose
  • We are having three service
    1. Mongodb Service
    2. User Service. => Depends on Mongodb service
    3. Gateway Service => Depends on User service
  • To learn more about repo read this article.

Step 4. Understand Docker-compose file.

  • In this file, we are having docker-compose manifest file.
  • This file just instructs the docker file on where to build and how to run.

                version: "3.9"

# From here service and docker commands starts
services:
  # Mongodb Container Info and commnads
  mongodb:  # name of service. This will be used by docker compose only
    image: mongo  # name of image. This will directly pull from container registry
    ports:
      - "27017:27017" # which port need to expose and connect
    container_name: mongodb # name of container

  # User Container Info and commnads
  user:  # User service. This will be used by docker compose only
    build: # Build file path info
      context: user # context to specify folder name where docker file is resides
      dockerfile: ./Dockerfile # name of docker file
    ports: # which port need to expose and connect
      - "8080:8000"
    container_name: user # name of container
    environment: # pass environment variable to user service
      - MONGODB_HOST=host.docker.internal:27017
    depends_on: # dependency for user service. Specify service will start first.
      - mongodb

  # Gateway Container Info and commnads
  gateway: # User service. This will be used by docker compose only
    build: # Build file path info
      context: gateway # context to specify folder name where docker file is resides
      dockerfile: ./Dockerfile # name of docker file
    environment: # pass environment variable to user service
      - USER_HOST=host.docker.internal:8080
    ports: # which port need to expose and connect
      - "3000:3000"
    container_name: gateway # name of container
    depends_on: # dependenies for gateway service. Specify service will start first.
      - mongodb
      - user
            

Step 5: Run these services and access them through localhost.

  • Run this following command to build and run docker files

                docker compose up -d
            

  • Next access our application from localhost:3000

Feedback: Thank you for reading this article. I hope you understood the basics of deploying your application from scratch with docker compose. Please feel free to ask questions or give suggestions to improve the content quality. Visit this for all articles.


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

Avatar

RAKESH KUMAR SHARMA

@airrakesh
Simplifying human life | 👨🏽‍💻
User Popularity
20

Influence

1k

Total Hits

2

Posts