DEV Community

YuIT Solutions
YuIT Solutions

Posted on

🐳Docker Compose is very simple👍

Introduction to Docker Compose

🐳Docker Compose is a powerful tool for defining and running multi-container Docker applications.
📑In this article, we will delve into the world of Docker Compose using a real-world example of a web application that consists of a PostgreSQL database, a backend service, and a frontend service.
👉In the previous article I have seen how use only docker for this

What is Docker Compose?

You just can read the official documentation😁
Or read a short description: Docker Compose is a command-line tool that allows you to define and run multi-container Docker applications. It uses a YAML file, known as the docker-compose.yml file, to define the services that make up your application. Docker Compose handles the creation and management of the containers, networks, and volumes required by your application.

Our example application consists of three services:

  • Database - PostgreSQL: This service is responsible for storing data for our application.
  • Backend - Nest.js: This service handles the business logic of our application and interacts with the database.
  • Frontend - Next.js: This service provides a user interface for our application and interacts with the backend service.

Docker Compose Configuration

❗Before we start, we need:

1️⃣ To clone examples repositories for:

  • BE: git clone https://github.com/YuesIt17/portfolio-yues-nestjs-docker.git
  • FE: git clone https://github.com/YuesIt17/portfolio-yues-nextjs-docker.git
  • Docker Compose: git clone https://github.com/YuesIt17/portfolio-yues-docker-config.git

2️⃣ To start the docker db container and create docker images for FE and BE. I describe this in my README:

3️⃣ To run the containers via: docker-compose up -d. If you wanna stop and remove the containers via:docker-compose down

4️⃣ You can then access the application by visiting http://localhost:3000 in your web browser

👀Here is the docker-compose.yml file that defines our example application:

version: "3.8"

services:
  db:
    image: postgres
    container_name: yuit-chart-db-host
    environment:
      POSTGRES_DB: yuit-chart-db
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: adminyuitpassword
    networks:
      - yuit-net
    ports:
      - "5432:5432"
    restart: unless-stopped

  backend:
    image: yuit-chart-backend-image:latest
    container_name: yuit-chart-backend
    networks:
      - yuit-net
    ports:
      - "127.0.0.1:8080:8080"
    depends_on:
      - db
    restart: unless-stopped

  frontend:
    image: yuit-chart-frontend-image:latest
    container_name: yuit-chart-frontend
    networks:
      - yuit-net
    ports:
      - "127.0.0.1:3000:8080"
    restart: unless-stopped

networks:
  yuit-net:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

💪Let's break down this configuration:

  • version directive specifies the version of the Docker Compose format.
  • services directive defines the services that make up our application.
  • db service uses the official PostgreSQL image and sets environment variables for the database name, user, and password.
  • backend service uses a custom image (yuit-chart-backend-image:latest) and depends on the db service.
  • frontend service uses a custom image (yuit-chart-frontend-image:latest) and does not depend on any other services.
  • networks directive defines a network named yuit-net with a bridge driver.

Key Concepts

Here are some key concepts to understand when working with Docker Compose:

  • Services: A service is a container that runs a specific image. In our example, we have three services: db, backend, and frontend.
  • Images: An image is a template for creating containers. In our example, we use the official PostgreSQL image and two custom images.
  • Networks: A network is a way for services to communicate with each other. In our example, we define a network named yuit-net with a bridge driver.
  • Environment Variables: Environment variables are used to configure services. In our example, we set environment variables for the database name, user, and password.
  • Depends On: The depends_on directive specifies that a service depends on another service. In our example, the backend service depends on the db service.

Conclusion

In this article, we introduced Docker Compose. We defined a docker-compose.yml file that specified three services: a PostgreSQL database, a backend service, and a frontend service. We then ran the application using the docker-compose up command. Docker Compose provides a powerful way to define and run multi-container Docker applications, making it an essential tool for developers and DevOps teams.

Thanks for reading and feedback!
See you soon!🤝

Top comments (0)