DEV Community


Posted on

A Docker Compose Example (FlaskAPP + PostgreSQL + Traefik).


Docker Compose is a tool used for defining and running multi-container Docker applications. It allows developers to describe all the services that make up an application in a single YAML file, defining the relationships and configurations between them. Docker Compose then uses this YAML configuration to deploy and manage the application's containers as a single unit. Docker Compose is particularly useful for development environments, testing, and deploying multi-container applications in a consistent and reproducible manner. It simplifies the process of managing complex application architectures by providing a declarative configuration file and a set of commands for common operations.

Components of Docker compose

  • compose file

A YAML file is used to define the configuration of the entire application stack, including services, networks, and volumes. It specifies details such as the Docker images to use, container dependencies, environment variables etc.

The default path for a Compose file is compose.yaml or compose.yml. docker-compose.yml/docker-compose.yaml also supported for backwards compatibility.

  • Services

Each service in a Docker Compose file represents a containerized application component. Services can be built from a specified Dockerfile or use pre-built images from a registry. You can define the relationships and dependencies between services.

  • Volumes

Volumes can be defined to persist data outside the containers. This is crucial for storing data that needs to survive container restarts or updates.

  • Environment Variables

Docker Compose allows you to set environment variables for services.

  • Networks

Docker Compose allows you to define custom networks to connect containers and control communication between them. Services can be attached to specific networks to isolate or expose their communication.

A Sample compose file

The Docker Compose file below defines a set of services that work together to run a Flask-based todo application along with a PostgreSQL database and Traefik as a reverse proxy/load balancer.

    image: tinegagideon/flask-todo
      - "5000:5000"
      - DATABASE_URL=postgresql+psycopg2://postgres:postgres@todo-db:5432/postgres
      # Explicitly tell Traefik to expose this container
      - "traefik.enable=true"
      # The domain the service will respond to
      - "traefik.http.routers.todo.rule=Host(`localhost`)"
      - todo-db
      - traefik

    image: postgres:14
      - "5432:5432"
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=postgres
      - pgdata:/var/lib/postgresql/data

    image: traefik:v3.0
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "80:80"
      - "8080:8080"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"


Enter fullscreen mode Exit fullscreen mode

Compose file breakdown

The compose file is divided into two sections: services and volume, with three services.

Services Section

todo Service

  • It uses an image named tinegagideon/flask-todo for the Flask todo application.
  • Exposes port 5000 of the container to port 5000 of the host machine.
  • Sets environment variables for the Flask application, including the database URL.
  • Uses labels to configure Traefik to expose this service and defines the routing rule.
  • Depends on the todo-db service and the traefik service.

todo-db Service

  • Uses the official PostgreSQL image with version 14.
  • Exposes port 5432 of the container to port 5432 of the host machine.
  • Sets environment variables for the PostgreSQL database i.e the password, username, and database name.
  • Mounts a volume named pgdata to persist PostgreSQL data.

traefik Service

  • Uses the Traefik image with version 3.0.
  • Specifies command-line arguments for Traefik:

enables insecure access to the Traefik API.

enables Docker as a provider.

specifies that services must be explicitly exposed.

defines the entry point for HTTP traffic.

  • Exposes port 80 and port 8080 of the container to the corresponding ports on the host machine.
  • Mounts the Docker socket to interact with Docker daemon inside the Traefik container.

Volumes Section

Defines a named volume named pgdata that is used by the todo-db service to persist PostgreSQL data.

Top comments (0)