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.
When we run a container from the command line, there are different options associated with the container:
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.
docker run -d \
-p 27017:27017 \
--name mongo \
--network mongo-network \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=qwerty \
mongoAs applications grow, these commands become longer and harder to manage.
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.
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.
| 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 |
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:
can be done directly inside the YAML file.
This makes maintenance much easier.
| 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 |
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.
Docker Compose provides:
Instead of running multiple Docker commands every time, developers define everything once in a YAML file and manage the entire application stack from there.
.yaml) configuration file.