Introduction to Docker Compose Running Multi Container Apps
Real applications are never just one container. A web application typically has a frontend, a backend API, a database, a cache, and a background worker — each running in its own container. Starting, connecting, and configuring five containers manually with long docker run commands every time is painful and error-prone. Docker Compose solves this by letting you describe your entire application stack in one file and start everything with a single command.
The Problem Docker Compose Solves
Without Docker Compose, starting a typical web app with a database and cache looks like this:
# Terminal — manual way (painful) docker network create app-net docker run -d \ --name postgres \ --network app-net \ -e POSTGRES_USER=admin \ -e POSTGRES_PASSWORD=secret \ -e POSTGRES_DB=myapp \ -v pgdata:/var/lib/postgresql/data \ postgres:15 docker run -d \ --name redis \ --network app-net \ redis:7 docker run -d \ --name webapp \ --network app-net \ -p 5000:5000 \ -e DATABASE_URL=postgres://admin:secret@postgres:5432/myapp \ -e REDIS_URL=redis://redis:6379 \ my-flask-app:1.0
That is a lot to type correctly every time. If a team member sets up the project, they must know all these commands and get every flag exactly right. One mistake and nothing works.
The Docker Compose Way
You write everything once in a compose.yaml file and then run:
docker compose up
Docker Compose reads the file, creates the network, starts all containers in the right order, passes all the environment variables, mounts all the volumes — automatically.
What Docker Compose Is
Docker Compose is a tool that manages multi-container Docker applications. It reads a YAML configuration file that describes your services, networks, and volumes. You define the desired state of your entire application, and Docker Compose makes it happen.
compose.yaml (one file)
↓
docker compose up
↓
┌────────────────────────────────────────────┐
│ Docker Compose creates: │
│ ✓ Network: app_default │
│ ✓ Volume: pgdata │
│ ✓ Container: postgres (with env + volume) │
│ ✓ Container: redis │
│ ✓ Container: webapp (with env + ports) │
│ All containers connected on app_default │
└────────────────────────────────────────────┘
Docker Compose vs Docker Swarm vs Kubernetes
┌──────────────────┬────────────────────────────────────────────┐ │ Docker Compose │ Single machine. Development and small │ │ │ production deployments. Easy to learn. │ ├──────────────────┼────────────────────────────────────────────┤ │ Docker Swarm │ Multiple machines. Simple clustering. │ │ │ Built into Docker, less popular today. │ ├──────────────────┼────────────────────────────────────────────┤ │ Kubernetes │ Multiple machines. Enterprise scale. │ │ │ Full-featured, complex. Industry standard. │ └──────────────────┴────────────────────────────────────────────┘
Docker Compose is not a replacement for Kubernetes. It runs on one machine. When your app needs to run across multiple servers for high availability and massive scale, you graduate to Kubernetes. For local development and single-server deployments, Docker Compose is perfect.
Installing Docker Compose
If you installed Docker Desktop (Windows or macOS), Docker Compose is already included. Run this to verify:
docker compose version Docker Compose version v2.24.0
On Linux with Docker Engine, install the Compose plugin:
sudo apt-get install docker-compose-plugin
Note the command difference: the modern version uses docker compose (with a space, as a Docker plugin). Older installations used docker-compose (with a hyphen, as a separate binary). This course uses the modern docker compose syntax.
Your First compose.yaml File
Create a new folder called my-compose-app. Inside it, create compose.yaml:
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: secret
Start both containers:
docker compose up
Docker Compose creates a default network, starts nginx on port 8080, and starts PostgreSQL. Both containers can reach each other using their service names (web and db). Stop everything with Ctrl+C, or run in the background with:
docker compose up -d
Stop and remove all containers, networks, and (optionally) volumes:
docker compose down docker compose down -v ← also removes volumes
Key Points
- Docker Compose manages multi-container apps using a single
compose.yamlfile. docker compose upstarts everything;docker compose downtears it all down.- Services in the same Compose file automatically share a network and resolve each other by service name.
- Docker Compose runs on one machine — use Kubernetes for multi-server deployments.
