Docker Compose: Run Multiple Containers Easily
Learn Docker Compose basics and how YAML configuration can manage multiple containers, networking, ports, volumes, and development environments.
Series: My 90-Day AWS & DevOps Learning Journey (18 articles)
- …
- 16Docker Compose: Run Multiple Containers Easily This article
Docker Compose: Run Multiple Containers Easily
What happens when your application needs more than one container?
For example, imagine a simple web application that needs:
1
2
3
4
5
Web Application
+
Database
+
Cache
Running each container manually can become difficult.
You might need to remember:
- Which containers to start
- Which ports to use
- Which networks they need
- Which environment variables are required
- Which container depends on another
This is where Docker Compose can make things easier.
What Is Docker Compose?
Docker Compose is a tool for defining and running multi-container applications using a configuration file.
Instead of manually running several Docker commands, you can describe your application services in a YAML file.
A simplified architecture might look like:
1
2
3
4
5
6
7
Docker Compose
|
+---+---+
| |
↓ ↓
Web Database
Container Container
This makes it easier to manage applications that consist of multiple containers.
A Simple Example
Imagine building a Python web application.
It needs:
1
2
3
Python Application
+
PostgreSQL Database
With Docker Compose, both services can be defined in one configuration.
A simplified
compose.yaml could look like:1
2
3
4
5
6
7
8
services:
web:
build: .
ports:
- "8000:8000"
database:
image: postgres:16
This file tells Compose that the application has two services:
webdatabase
The actual configuration would normally include additional settings such as environment variables, volumes, health checks, and networking.
Why Use a YAML File?
One of the useful ideas behind Docker Compose is configuration as code.
Instead of remembering a long list of Docker commands, the application's configuration can be stored in a file.
For example:
1
2
3
4
5
compose.yaml
↓
Application Configuration
↓
Git Repository
The file can also be version-controlled with Git.
This is very useful in DevOps because infrastructure and application configuration can be treated as code.
Starting the Application
Once the Compose configuration is ready, you can start the application with:
1
docker compose up
Compose reads the configuration and starts the required services.
To run the services in the background:
1
docker compose up -d
To stop the application:
1
docker compose down
This is much easier than manually starting and stopping every container.
How Containers Communicate
When multiple services run together, they often need to communicate with each other.
For example:
1
2
3
4
Web Container
|
↓
Database Container
Docker Compose creates networking that allows services in the same Compose application to communicate.
The application can typically refer to another service using its service name rather than relying on a hard-coded IP address.
For example:
1
database
can be used as the database hostname when
database is the service name.This makes the configuration easier to manage.
Ports
Containers can expose services through ports.
For example:
1
2
ports:
- "8000:8000"
This means traffic arriving at port
8000 on the host can be forwarded to port 8000 inside the container.A simplified view:
1
2
3
4
5
6
7
Browser
↓
localhost:8000
↓
Docker Host
↓
Web Container
Port mapping is an important concept when running web applications locally.
Environment Variables
Applications often need configuration such as:
- Database hostname
- Database username
- Application mode
- Port numbers
- API configuration
Docker Compose can provide environment variables to containers.
For example:
1
2
environment:
APP_ENV: development
For sensitive credentials, however, avoid putting passwords or API keys directly into a public Compose file.
Use an appropriate secrets-management approach for the environment.
Volumes and Data
Containers are often treated as replaceable.
But databases need persistent data.
Docker volumes can help store data outside the container's writable layer.
For example:
1
2
3
4
5
Database Container
↓
Docker Volume
↓
Persistent Data
If the database container is removed and recreated, the volume can preserve the data when configured correctly.
This is an important concept when running stateful applications.
Docker Compose in Development
Docker Compose is especially useful for local development.
Imagine a developer needs:
1
2
3
4
Frontend
Backend
Database
Redis
Instead of installing and configuring everything directly on the laptop, the developer can define the services using Compose.
The workflow becomes:
1
2
3
4
5
6
7
8
9
Clone Project
↓
Read compose.yaml
↓
docker compose up
↓
All Required Services
↓
Start Development
This can make onboarding new developers easier.
Docker Compose and DevOps
Docker Compose also introduces several DevOps concepts:
- Configuration as code
- Reproducible environments
- Multi-container applications
- Service dependencies
- Networking
- Environment configuration
- Automation
A simple development workflow could be:
1
2
3
4
5
6
7
8
9
10
11
Git
↓
Clone Repository
↓
Docker Compose
↓
Start Application
↓
Run Tests
↓
Development
This is a good stepping stone toward understanding more advanced container orchestration platforms.
Docker Compose vs Docker
Docker and Docker Compose are related, but they solve different problems.
Docker can build and run individual containers.
Docker Compose helps define and manage applications containing multiple services.
Think of it like:
1
2
3
4
5
Docker
→ Manage Containers
Docker Compose
→ Manage Multi-Container Applications
Docker Compose vs Kubernetes
As you continue learning DevOps, you may hear about Kubernetes.
Docker Compose is commonly used for local development and simpler multi-container environments.
Kubernetes is a much larger container orchestration platform designed for managing containerized workloads at scale.
A useful learning progression can be:
1
2
3
4
5
6
7
8
9
Docker
↓
Docker Compose
↓
Container Networking
↓
CI/CD
↓
Kubernetes
You don't need to learn everything at once.
Understanding Docker fundamentals first makes the next steps easier.
What I'm Learning From Docker Compose
One thing I find useful about Compose is the idea of describing an environment instead of manually creating it every time.
Rather than saying:
"Run this container, then configure that container, then connect them."
I can define the environment as configuration and let Docker Compose create it.
That is a small but important step toward thinking like a DevOps engineer.
💬 Let's Discuss
For developers and DevOps learners:
When you first started using Docker, did you find single-container applications easy, or did networking multiple containers together become the difficult part?
If you've used Docker Compose, what was the first multi-container project you built?
I'd love to learn from different experiences and see what other beginners are currently building.
Key Takeaway
Docker Compose makes it easier to define and manage applications that contain multiple services.
The basic idea is:
1
2
3
4
5
6
7
compose.yaml
↓
Multiple Services
↓
Multiple Containers
↓
One Application
For someone learning DevOps, Docker Compose is a useful bridge between basic Docker containers and larger container orchestration concepts.
Series: My 90-Day AWS & DevOps Learning Journey (18 articles)
- …
- 16Docker Compose: Run Multiple Containers Easily This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article