Docker Compose
Now we are going to talk about an important tool called Docker Compose.
Basically, whenever we run multiple containers, for the ease of running those containers as a developer, we have an additional tool available called Docker Compose.
Everything we did with multiple containers using Docker commands was fine — but Docker Compose makes that same work a little easier for us.
What Does Docker Compose Do?
When we run a container from the command line, there are different options associated with the container:
- Port Binding
- Custom Name
- Network Configuration
- Environment Variables
- Volume Configuration
- Restart Policies
- And many more settings
This process of running Docker containers from the command line can become quite complex.
Instead of running Docker commands on the terminal, we can convert all our commands into a single file.
This file is a .yaml file (YAML — Yet Another Markup Language) in which we basically convert and write all our Docker commands.
Instead of running containers from the terminal, we now run them from this file.
Traditional Docker Approach
docker run -d \
-p 27017:27017 \
--name mongo \
--network mongo-network \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=qwerty \
mongo
As applications grow, these commands become longer and harder to manage.
Docker Compose Approach
Instead of writing long commands in the terminal, all configurations are stored inside a YAML file.
Example:
services:
mongo:
image: mongo
ports:
- "27017:27017"
Then the containers can be started using a single command.
Two Benefits of Using a YAML File
1. Structured and Standardized Configuration
Whatever Docker commands we need to run are available in a very structured and standardized way.
Instead of remembering long terminal commands, all configuration is stored in one place.
Example
| Terminal Command | YAML Configuration |
|---|
| Long and difficult to read | Easy to read |
| Hard to remember | Self-documenting |
| Configuration scattered in terminal history | Configuration stored in a file |
2. Easier Maintenance and Updates
Whenever we need to make edits inside this file, doing so is very easy compared to editing a long Docker command on the terminal.
For example, changing:
- Port numbers
- Environment variables
- Container names
- Networks
- Volumes
can be done directly inside the YAML file.
This makes maintenance much easier.
Docker Commands vs Docker Compose
| Feature | Docker Commands | Docker Compose |
|---|
| Configuration Location | Terminal Commands | YAML File |
| Readability | Lower | Higher |
| Reusability | Limited | High |
| Maintenance | Difficult | Easy |
| Multiple Containers | Complex | Simple |
| Team Collaboration | Harder | Easier |
| Version Control | Not Practical | Can be committed to Git |
When Does Docker Compose Make Sense?
Docker Compose generally makes sense when we have multiple containers.
In practical real-life applications, multiple containers are running together at the same time — that is where Docker Compose is most helpful.
Examples:
| Application | Containers |
|---|
| MERN Application | React + Node.js + MongoDB |
| E-Commerce Application | Frontend + Backend + Database + Redis |
| Microservices Application | Multiple Services + Databases |
| Monitoring Stack | Prometheus + Grafana + Alert Manager |
Managing all these containers individually through Docker commands can become difficult.
Docker Compose allows us to manage them together from a single configuration file.
Why Developers Prefer Docker Compose
Docker Compose provides:
- Simpler container management
- Centralized configuration
- Better readability
- Easier maintenance
- Faster project setup
- Better collaboration among team members
Instead of running multiple Docker commands every time, developers define everything once in a YAML file and manage the entire application stack from there.
Summary
- Docker Compose is a tool for managing multiple Docker containers.
- It uses a YAML (
.yaml) configuration file.
- Container settings such as ports, networks, names, and environment variables can be defined in one place.
- Configuration becomes easier to read, edit, and maintain.
- Docker Compose is most useful in applications where multiple containers need to run together.
- It simplifies development workflows and improves team collaboration.