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.

Summary

Docker Compose combines a YAML configuration file and a CLI tool to orchestrate multiple interconnected Docker containers. Most real-world software solutions require multiple services running in separate containers (databases, proxies, web servers, etc.), which Docker Compose simplifies by allowing you to define and manage all containers, networks, volumes, and environment variables in a single file. The tool is primarily used for local development and testing, though version 1.13+ supports production deployments with Docker Swarm.

Key points

  • Docker Compose consists of two parts: a YAML configuration file and the docker-compose CLI tool used for local development and testing
  • It solves the problem of running and networking multiple interdependent containers without manually remembering all docker run options and network configuration
  • The YAML file is where you specify all containers, networks, volumes, environment variables, and images needed for your application
  • Docker Compose supports version declarations, with evolution from v1 (originally called 'fig') through v1.13+ which can be used directly with docker CLI for production deployments with Swarm
  • The default filename is docker-compose.yaml, but you can specify custom filenames using the -f flag

FAQ

What is Docker Compose and why is it needed?

Docker Compose is a tool that combines a YAML configuration file and a CLI utility to manage multiple interconnected Docker containers. Since most real-world applications require multiple services (databases, proxies, web servers, etc.), Docker Compose allows you to define and control all of them together instead of manually running separate docker run commands with correct network and volume setup.

What are the two components of Docker Compose?

The two components are: (1) the YAML configuration file where you specify all containers, networks, volumes, and environment variables, and (2) the docker-compose CLI tool which uses that file to simplify management of your containerized application.

Can Docker Compose be used in production?

Starting from version 1.13 onwards, Docker Compose files can be used directly with the standard docker CLI command for production deployments with Docker Swarm. However, the docker-compose tool itself is primarily designed for local development and testing.