Docker Curriculum
What Is Docker Compose
Running a single container is straightforward. Running a real application is not. A production web application typically involves a web server, an API, a database, a cache, a message queue, a background worker, and monitoring agents. Starting each container individually — with the right flags, the right network, the right volumes, the right environment variables, in the right order — is tedious, error-prone, and impossible to reproduce reliably.
Docker Compose solves this. It is a tool that lets you define and run multi-container applications using a single YAML configuration file. You describe every service, network, and volume your application needs in that file, and Docker Compose handles the rest.
The Problem Compose Solves
Without Compose, running a Python web app with PostgreSQL and Redis requires:
You need to run this exact sequence every time. If you change a port, a password, or add a service, you have to manually update multiple commands. If a colleague wants to run this locally, they need all these commands too.
With Compose, all of this becomes a single file:
And a single command to start everything:
What Compose Does Automatically
When you run docker compose up, Compose:
- Reads
docker-compose.ymlin the current directory - Creates a project network — a custom bridge network named
<project>_default, all services join it automatically - Creates declared volumes — named volumes defined in the
volumes:block are created if they do not exist - Starts services in dependency order — if service B
depends_onservice A, Compose starts A first - Pulls missing images — if an image is not cached locally, Compose pulls it
- Names containers — containers are named
<project>_<service>_<index>(e.g.,myapp_db_1)
All services on the project's network can resolve each other by service name. The api service connects to the database using the hostname db — Compose's DNS handles the rest.
Compose vs docker run — When to Use Each
Rule of thumb: Any time your application involves more than one container that needs to work together, use Docker Compose.
The Compose File Location and Naming
By default, docker compose looks for a file named docker-compose.yml or docker-compose.yaml in the current directory. You can specify a different file with -f:
Compose v1 vs Compose v2 (Modern Compose)
Docker Compose has two versions to be aware of:
Compose v1 (docker-compose — note the hyphen)
- Written in Python, installed as a separate binary
- Command:
docker-compose up - Being phased out — EOL since July 2023
Compose v2 (docker compose — note the space)
- Written in Go, integrated directly into the Docker CLI as a plugin
- Command:
docker compose up - Current standard — use this
All examples in this module use Compose v2 (docker compose).
Compose in Production
Docker Compose is most commonly used for development and single-host production deployments. For multi-host, highly-available production deployments, orchestrators like AWS ECS, Kubernetes (EKS), or Docker Swarm handle container scheduling, scaling, and health management.
That said, many small-to-medium scale production deployments run Docker Compose on a single EC2 instance and that is entirely legitimate. A well-configured Compose file with restart: unless-stopped on all services provides solid reliability for applications that do not need horizontal scaling.
Summary
- Docker Compose is a tool for defining and running multi-container applications using a single YAML file (
docker-compose.yml) - It automates network creation, volume creation, dependency ordering, and container naming — everything you would do manually with multiple
docker runcommands docker compose up -dstarts the entire application stack in the background;docker compose downstops and removes everything- All services defined in the same Compose file automatically join a shared custom bridge network and can resolve each other by service name
- Use Compose v2 (
docker composewith a space) — Compose v1 (docker-composewith a hyphen) is deprecated and end-of-life - Compose is appropriate for development environments, single-host production, and any application that needs multiple cooperating containers
In the next file, we cover the structure of the Compose file in depth — every top-level key and what belongs inside each section.