Docker Compose is a container orchestration tool designed to define, configure, and run multi-service applications using a single YAML configuration file.
Up to this point in the course, we have been terminal craftsmen. To launch a complete web application (say, a WordPress with its database), we had to perform several actions:
- Create a manual network:
docker network create... - Launch the database:
docker run -d --name db --network... -e PASSWORD... - Launch WordPress:
docker run -d -p 80:80 --network... -e DB_HOST...
And if we made a mistake in one of them, in a variable, or in the order, you had to start from scratch.
This is where Docker Compose comes in. The tool that allows us to stop executing individual commands and start defining our entire architecture in a single file.
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications.
Instead of using the CLI (command line) to launch each container separately, we use a YAML file (called docker-compose.yml) where we write all the configuration of our services.
With a single command (docker compose up), Docker reads that file and automatically:
- Creates the necessary networks.
- Creates the volumes.
- Starts all containers in the correct order.
- Connects everything together.
Infrastructure as Code (IaC)
When discussing Dockerfile, we already saw the concept of Infrastructure as Code. This takes it a step further.
With Docker Compose, we move from an imperative approach to a declarative one. What’s that? 👇
- Imperative (Before): You tell the machine what to do step by step. “Download this, then create the network, then open the port…”. If you skip a step, it fails.
- Declarative (Docker Compose): You describe the desired final state to the machine. “I want a system that has a WordPress and a MySQL connected.” Docker takes care of calculating the necessary steps to reach that state.
Advantages of having your infrastructure in a file
Still think the IaC benefits are not enough? Here are a few more.
- Versioning (Git): Being a text file (
.yml), you can upload yourdocker-compose.ymlto GitHub. You have a change history of your infrastructure just like your code. - Reproducibility: Your colleague downloads the repository, runs
docker compose up, and has exactly the same environment as you. - Documentation: The file perfectly describes what ports are used, what volumes are mounted, and what passwords (variables) are needed.
What does a docker-compose.yml file look like?
Although we will dive into the syntax in the next article, let’s see an example so you can see how convenient it is.
Here is what a complete WordPress + Database environment looks like:
version: '3.8'
services:
database:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: secret
volumes:
- db_data:/var/lib/mysql
wordpress:
image: wordpress:latest
ports:
- "8080:80"
depends_on:
- database
volumes:
db_data:
Do you see how clear it is?
- We have two services:
databaseandwordpress. - We clearly see which image each one uses.
- We see that WordPress depends on the database.
- We see that port 8080 is open.
The Commands
Once you have this Docker Compose file, using it is very simple. You only need two basic commands:
Start everything (up)
docker compose up -d
The -d (Detached) flag still works here. Docker will read the file, download images, create networks, and start everything in the background.
Stop and clean everything (down)
docker compose down
This doesn’t just stop the containers (like stop), but deletes the containers and networks it created. It leaves your system clean. (Note: For safety, it does not delete data volumes unless you explicitly ask it to).
Docker Compose vs Docker Swarm / Kubernetes
There are many tools for container orchestration, and it’s easy to confuse them.
- Docker Compose: Used to orchestrate containers on a single node (your computer or a VPS server). It’s perfect for development, testing, and small/medium productions.
- Kubernetes / Swarm: Used to orchestrate containers on a cluster of many servers.
For 99% of developers and personal projects, Docker Compose is all you need.
