DOCKER - 6 1 Docker Compose Presentation

Welcome to this introduction to Docker Compose. Until now, we have started containers one by one with docker run. But real applications often need several containers working together: a web server, a database, a cache. Starting and connecting them all by hand quickly becomes tedious. Docker Compose solves exactly this problem.

What is Docker Compose

Docker Compose is a tool for defining and running multi-container applications. Instead of typing many docker run commands, you describe your whole application — its services, networks and volumes — in a single configuration file named docker-compose.yml. Then a single command starts everything.

The docker-compose.yml file

The configuration is written in YAML. Each container is declared as a service, with its image, its ports, its environment variables, and so on. Here is a simple example with a web server and a database:

services:
  web:
    image: nginx
    ports:
      - "8080:80"
  database:
    image: postgres
    environment:
      POSTGRES_PASSWORD: secret

The main commands

  • docker compose up : builds, creates and starts all the services defined in the file.
  • docker compose up -d : the same, in detached (background) mode.
  • docker compose down : stops and removes the containers, networks and (optionally) volumes.
  • docker compose ps : lists the running services of the project.

Another key benefit: Compose automatically creates a dedicated network for the project, so the services can reach each other simply by their service name (for example, the web service can connect to the database service using the hostname database). In the next lessons, we will explore the structure of the docker-compose.yml file in more detail.