Skip to content

Docker Compose

Docker Compose is a powerful tool that simplifies the definition and execution of multi-container Docker applications, allowing developers to easily orchestrate and manage complex environments. Using a single YAML configuration file, known as docker-compose.yml, users can specify all the services that make up their application, including images, environment variables, volumes, and networks. This facilitates development, testing, and deployment, as Docker Compose allows all containers to be started with a single command (docker compose up), ensuring that all necessary services are configured and run consistently. Furthermore, the ability to scale services, share configurations between environments, and integrate with CI/CD tools makes Docker Compose a popular choice in modern software development workflows.

mmd

Fundamental Concepts

1. Definition

  • Docker Compose is a tool that allows you to define and run Docker applications composed of multiple containers. It uses a configuration file (usually named docker-compose.yml) where you describe the services that make up your application.

2. docker-compose.yml File

  • The docker-compose.yml file is where you define all the services, networks, and volumes needed for your application. The syntax is based on YAML, which makes the configuration quite readable.

Basic Structure of docker-compose.yml

services:     # Definition of services
  web:        # Service name
    image: nginx:latest  # Image to be used
    ports:
      - "80:80"  # Port mapping
  db:         # Another service (database)
    image: postgres:latest
    environment:  # Environment variables
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password

Key Components

1. Services

  • Each service in docker-compose.yml represents a container. You can specify the image to be used, network settings, environment variables, volumes, and much more.

2. Networks

  • Compose creates a default network for services to communicate with each other. You can customize networks, allowing services on different networks to communicate or isolating services as needed.

3. Volumes

  • Volumes allow you to persist data generated by containers. They are stored outside the container's lifecycle, ensuring that data is not lost when a container is removed or updated.

Main Docker Compose Commands

1. docker compose up

  • This command creates and starts all services defined in the docker-compose.yml file.
  • Example:
    docker compose up
    

2. docker compose down

  • Stops all services and removes the associated containers, networks, and volumes.
  • Example:
    docker compose down
    

3. docker compose build

  • This command builds or rebuilds the services defined in the Compose file.
  • Example:
    docker compose build
    

4. docker compose logs

  • Shows the logs from all running services, allowing you to monitor the output of the containers.
  • Example:
    docker compose logs
    

5. docker compose exec

  • Allows you to run commands inside a running container.
  • Example:
    docker compose exec web bash
    

Advantages of Docker Compose

1. Simplicity

  • Docker Compose simplifies the management of multi-container applications by allowing you to define all services in a single configuration file.

2. Consistency

  • The application configuration can be easily shared across different environments (development, testing, production), ensuring that everyone is using the same setup.

3. Ease of Use

  • Docker Compose provides an intuitive command-line interface that makes it easy to start, stop, and manage complex applications.

4. Orchestration

  • Although Docker Compose is not a full-fledged orchestration tool like Kubernetes, it provides basic orchestration functionalities for applications that do not require the complexity of a more robust solution.

Example of Docker Compose for the Project

Below is an example of how Docker Compose is used in our project to orchestrate the microservices:

services:
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: root
      POSTGRES_DB: feedback
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

  usermanagement:
    build:
      context: ./java_backend
      args:
        APP_NAME: usermanagement
    environment:
      - DB_HOST=db
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_PASS=root
      - JWT_SECRET_KEY=4Z^XrroxR@dWxqf$!@#qGr4P
      - JWT_ISSUER=user-api
    depends_on:
      - db

  feedbackrequest:
    build:
      context: ./java_backend
      args:
        APP_NAME: feedbackrequest
    environment:
      - DB_HOST=db
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_PASS=root
      - JWT_SECRET_KEY=4Z^XrroxR@dWxqf$!@#qGr4P
      - JWT_ISSUER=user-api
    depends_on:
      - db

  feedbackresponse:
    build:
      context: ./java_backend
      args:
        APP_NAME: feedbackresponse
    environment:
      - DB_HOST=db
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_PASS=root
      - JWT_SECRET_KEY=4Z^XrroxR@dWxqf$!@#qGr4P
      - JWT_ISSUER=user-api
    depends_on:
      - db

  feedbackresponseview:
    build:
      context: ./python_backend
      args:
        APP_NAME: feedbackresponseview
    environment:
      - DATABASE_URL=postgresql://postgres:root@db:5432/feedback
    depends_on:
      - db

  nginx:
    image: nginx:alpine
    volumes:
      - ./gateway/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "80:80"
    depends_on:
      - usermanagement
      - feedbackrequest
      - feedbackresponse
      - feedbackresponseview

volumes:
  postgres_data:

This Docker Compose file defines:

  1. A PostgreSQL database service
  2. Four microservices (usermanagement, feedbackrequest, feedbackresponse, feedbackresponseview)
  3. An Nginx service as an API Gateway
  4. A volume to persist PostgreSQL data

Each service has its own configurations, such as environment variables, dependencies, and port mappings. Docker Compose manages the creation, startup, and interconnection of these services, allowing the entire application to be run with a single command.

Conclusion

Docker Compose is an essential tool for the development and deployment of microservices-based applications. It significantly simplifies the process of configuring and managing multiple containers, allowing developers to focus more on business logic and less on infrastructure.

In the context of our project, Docker Compose allows all microservices, the database, and the API Gateway to be configured and run consistently in any environment, facilitating the development, testing, and deployment of the application.