G: Managing a single Docker container is often straightforward. But what happens when your application grows into a complex ecosystem of a web server, a database, a cache, and maybe a few background workers? Suddenly, orchestrating them, linking them, and ensuring they all start and stop together becomes a challenge. Enter Docker Compose! 🚀✨
Docker Compose is a powerful tool designed to simplify the definition and running of multi-container Docker applications. With a single docker-compose.yml
file, you can configure your application’s services, networks, and volumes, and then use a handful of commands to bring your entire stack to life.
This comprehensive guide will walk you through the most essential Docker Compose commands, providing clear explanations, practical examples, and tips to help you master multi-container orchestration. Let’s dive in!
1. The docker-compose.yml
– Your Orchestration Blueprint 🏗️
Before we explore the commands, it’s crucial to understand the heart of Docker Compose: the docker-compose.yml
file. This YAML file describes your application’s services, defining everything from the Docker images to use, exposed ports, volume mounts, and environment variables.
Here’s a simple example for a Python Flask web app with a PostgreSQL database:
# docker-compose.yml
version: '3.8' # Specifies the Compose file format version
services:
web: # Define a service named 'web'
build: . # Build the Docker image from the current directory (where Dockerfile is)
ports:
- "5000:5000" # Map host port 5000 to container port 5000
volumes:
- .:/app # Mount the current directory into the container's /app
environment:
DATABASE_URL: postgres://user:password@db:5432/mydatabase # Env var for web service
depends_on: # Ensures 'db' service starts before 'web'
- db
command: python app.py # Command to run when the container starts
db: # Define a service named 'db'
image: postgres:13 # Use the official PostgreSQL 13 image from Docker Hub
environment: # Environment variables for the PostgreSQL container
POSTGRES_DB: mydatabase
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- db_data:/var/lib/postgresql/data # Mount a named volume for persistent data
volumes: # Define named volumes (managed by Docker)
db_data:
With this file in place, you’re ready to use Docker Compose commands to manage your entire application stack.
2. The Essentials: Getting Your Services Running and Monitored ⬆️⬇️📊📜
These are the commands you’ll use most frequently to start, stop, and observe your application.
2.1 docker compose up
– The Master Switch ⬆️✨
This is the command that brings your entire Docker Compose application to life. It builds, creates, starts, and attaches to containers for all the services defined in your docker-compose.yml
file.
-
Basic Usage:
docker compose up
This will start all services in the foreground, showing logs from all containers. To stop, press
Ctrl+C
. -
Run in Detached Mode (Background):
docker compose up -d
The
-d
(or--detach
) flag runs the containers in the background, allowing you to continue using your terminal. This is ideal for production environments or when you just want to get your services running quickly. -
Rebuild Images Before Starting:
docker compose up --build
If you’ve made changes to your
Dockerfile
or the source code that thebuild
context depends on,--build
forces Docker Compose to rebuild the service’s images before starting. -
Force Re-creation of Containers:
docker compose up --force-recreate
This option forces Compose to stop and recreate all containers, even if they haven’t changed. Useful for ensuring a clean slate or when debugging container-specific issues.
-
Specify a Service (Partial Start):
docker compose up -d web
You can specify one or more service names to start only those services and their dependencies. Here, only the
web
service (anddb
becauseweb
depends on it) would be started.
2.2 docker compose down
– Graceful Shutdown 👋⬇️
When you’re done with your application or need to stop it cleanly, down
is your go-to command. It stops containers and removes containers, networks, and (by default) the default network created by up
.
-
Basic Usage:
docker compose down
This will stop and remove the containers and the default network.
-
Remove Volumes (Use with Caution!):
docker compose down --volumes
The
--volumes
flag is crucial when you want to remove named volumes declared in yourdocker-compose.yml
(likedb_data
in our example). Be careful, as this will delete your persistent data! ⚠️ -
Remove Images as well:
docker compose down --rmi all
This will remove the images used by your services after stopping the containers. Useful for cleaning up disk space.
2.3 docker compose ps
– Check Your Services’ Status 📊🔍
Need to see what services are running and their current state? ps
is your friend.
-
Basic Usage:
docker compose ps
This will display a list of services defined in your
docker-compose.yml
, their status, ports, and the command they are running.NAME COMMAND SERVICE STATUS PORTS mywebapp-db-1 "docker-entrypoint.s…" db running 5432/tcp mywebapp-web-1 "python app.py" web running 0.0.0.0:5000->5000/tcp
2.4 docker compose logs
– Peek into Container Output 📜👀
Viewing logs is essential for debugging and monitoring. The logs
command fetches the output of your services.
-
Basic Usage (All Services):
docker compose logs
This will display logs from all services in your application stack.
-
Follow Logs (Real-time):
docker compose logs -f
The
-f
(or--follow
) flag streams the logs in real-time, similar totail -f
. PressCtrl+C
to stop following. -
View Logs for a Specific Service:
docker compose logs -f web
You can specify one or more service names to view logs only from those services.
-
Show Last N Lines:
docker compose logs --tail 100 web
The
--tail
flag limits the output to the last N lines. Useful for quick checks without overwhelming your terminal.
3. Managing Your Services: Control and Maintenance 🛠️
Once your services are running, you often need to manage them individually or collectively.
3.1 docker compose start
– Fire Up Stopped Services ▶️
If you’ve stopped services using docker compose stop
(not down
), start
will bring them back online without recreating them.
-
Start All Services:
docker compose start
-
Start Specific Services:
docker compose start web db
3.2 docker compose stop
– Pause Your Services ⏹️
This command stops running containers without removing them. Their state and data in volumes are preserved.
-
Stop All Services:
docker compose stop
-
Stop Specific Services:
docker compose stop web
3.3 docker compose restart
– Give Them a Fresh Start 🔄
As the name suggests, this command stops and then starts services. Useful for applying configuration changes that require a restart.
-
Restart All Services:
docker compose restart
-
Restart Specific Services:
docker compose restart web
3.4 docker compose build
– Rebuild Your Images 🏗️
When you update your Dockerfile
or the source code it builds from, you’ll need to rebuild the associated images.
-
Build All Service Images:
docker compose build
-
Build Specific Service Images:
docker compose build web
-
Disable Cache During Build:
docker compose build --no-cache web
The
--no-cache
flag forces a fresh build without using any cached layers. Handy for troubleshooting build issues or ensuring you’re pulling the latest dependencies.
3.5 docker compose rm
– Clean Up Stopped Containers 🗑️
This command removes stopped service containers. It does not remove volumes or networks.
-
Remove All Stopped Containers (Interactive):
docker compose rm
This will prompt you for confirmation before removing.
-
Force Remove Without Confirmation:
docker compose rm -f
-
Remove Specific Stopped Container:
docker compose rm web
4. Interacting with Your Services: Deeper Control 🧑💻🏃♂️🧐
Sometimes you need to get inside a container or run one-off tasks.
4.1 docker compose exec
– Run Commands Inside a Running Container 🧑💻
This is incredibly useful for debugging or performing administrative tasks within a running container.
-
Access the Shell of a Service:
docker compose exec web bash # For Linux-based containers docker compose exec web sh # For Alpine/BusyBox based containers
This opens an interactive shell session inside the
web
service container. You can then run commands likels
,pwd
, or check configurations. -
Run a Specific Command:
docker compose exec db psql -U user -d mydatabase
This runs the
psql
command directly inside thedb
container to connect to your database.
4.2 docker compose run
– One-Off Commands or Temporary Services 🏃♂️
Unlike exec
, run
starts a new container based on a service’s configuration and runs a specified command. This is perfect for one-off tasks like database migrations, cron jobs, or testing scripts.
-
Run a Database Migration:
docker compose run --rm web python manage.py migrate
The
--rm
flag ensures the container is removed automatically after the command finishes. Here, a newweb
container is spun up just to run themigrate
command and then removed. -
Start a Service Temporarily with Override:
docker compose run --rm web bash
This will start a new
web
container, but instead of runningpython app.py
, it will drop you into abash
shell. Other services (likedb
) will still be linked.
4.3 docker compose config
– Validate Your Configuration 🧐
Before running up
, it’s a good idea to validate your docker-compose.yml
file.
-
Validate and Display Configuration:
docker compose config
This command parses your
docker-compose.yml
and prints the effective configuration. It’s great for spotting syntax errors or understanding how Compose interprets your file. -
Validate Only:
docker compose config --quiet
The
--quiet
flag only returns an exit code:for valid,
1
for invalid.
4.4 docker compose pull
– Download Service Images ⬇️📦
This command downloads (pulls) the Docker images for all services defined in your docker-compose.yml
that specify an image
(not build
).
-
Pull All Images:
docker compose pull
-
Pull Specific Service Images:
docker compose pull db
5. Advanced & Less Common, But Useful Commands 🌐
While the previous commands cover most daily operations, these can be handy in specific scenarios.
5.1 docker compose pause
/ docker compose unpause
⏸️⏩
pause
: Pauses all running processes within a service’s container(s). This is different fromstop
as the container remains allocated and in memory.docker compose pause web
unpause
: Unpauses a paused service.docker compose unpause web
5.2 docker compose kill
💥
Sends a SIGKILL signal to containers to immediately stop them. Use this only as a last resort if stop
doesn’t work.
- Kill All Services:
docker compose kill
- Kill Specific Service:
docker compose kill web
5.3 docker compose port
🚪
Prints the public port for a service port mapping.
- Get Mapped Port:
docker compose port web 5000
This would output
0.0.0.0:5000
or just5000
depending on the binding.
5.4 docker compose top
📊
Displays the running processes of your services. Similar to docker top
but for Compose services.
- Show Processes:
docker compose top
5.5 docker compose events
📢
Listens for container events in real time. Useful for monitoring the lifecycle of your services.
- Watch Events:
docker compose events
6. Important Note: docker-compose
vs. docker compose
💡🔄
You might have noticed that all the examples above use docker compose
(without the hyphen). This is the modern syntax!
Historically, Docker Compose was a separate Python-based tool, and its command was docker-compose
. However, since Docker Desktop 3.0+ and Docker Engine 20.10+, docker compose
has been integrated directly into the Docker CLI as a plugin.
- Old syntax (standalone binary):
docker-compose up
- New syntax (CLI plugin):
docker compose up
While the old docker-compose
binary might still work on your system, it’s generally recommended to use the new docker compose
plugin for consistency, better integration, and to stay current with Docker’s development. All commands and flags remain virtually the same.
Conclusion 🎉
Docker Compose is an indispensable tool for anyone working with multi-container Docker applications. By understanding and mastering these core commands, you can efficiently define, run, and manage complex application stacks with ease. From developing locally to setting up continuous integration environments, Docker Compose streamlines your workflow, saving you time and effort.
Now that you have this complete guide, go ahead and experiment with your own docker-compose.yml
files. Practice makes perfect, and soon you’ll be orchestrating your Dockerized applications like a pro! Happy containerizing! ✨🐳