Docker Compose Commands
In the previous guide, we explored writing a Docker Compose file to define services, networks, and volumes for multi-container applications. Building on that foundation, this guide focuses on managing Docker Compose with CLI commands, allowing you to efficiently deploy, monitor, and scale applications. Whether you’re starting services, debugging configurations, or scaling containers, mastering these commands is key to streamlined container orchestration.
In this section, we’ll cover the following topics:
- Managing Docker Compose with CLI
- Summary of Docker Compose Commands
Managing Docker Compose with CLI
Docker Compose provides a powerful command-line interface (CLI) to manage multi-container applications efficiently. Below is a comprehensive guide to the most commonly used docker-compose
commands, categorized by their functionality.
Building and Running Services
1. docker-compose build
Builds or rebuilds the services defined in the docker-compose.yml
file.
Command Syntax:
docker-compose build [options]
Common Options:
--no-cache
: Forces a fresh build without using cached layers.--pull
: Pulls the latest version of the base image.
Example:
docker-compose build --no-cache
Explanation:
Builds all services from the docker-compose.yml file, forcing a fresh rebuild without using cached layers.
2. docker-compose up
Starts containers for the services defined in the docker-compose.yml
file, creating them if necessary.
Command Syntax:
docker-compose up [options]
Common Options:
-d
: Runs containers in detached mode (background).--build
: Forces a build before starting the containers.
Example:
docker-compose up -d
Explanation:
Starts all services in the background and keeps them running.
Service and Container Management
3. docker-compose ps
Displays the status of the running containers in the project.
Command Syntax:
docker-compose ps
Common Options:
-a
: Shows all containers, including stopped ones.
Example:
docker-compose ps -a
Explanation:
Lists all containers, including those that are stopped. Shows the container names, IDs, ports, and statuses.
4. docker-compose start
Starts containers that have been stopped without recreating them.
Command Syntax:
docker-compose start [service_name]
Example:
docker-compose start web
Explanation:
Starts the web
service if it was previously stopped.
5. docker-compose stop
Stops running containers gracefully without removing them.
Command Syntax:
docker-compose stop [service_name]
Example:
docker-compose stop
Explanation:
- If a service name is specified, only that service is stopped.
- If no service name is given, all running containers in the project are stopped.
6. docker-compose restart
Restarts one or more containers in the project.
Command Syntax:
docker-compose restart [service_name]
Example:
docker-compose restart db
Explanation:
Restarts the db
service container.
7. docker-compose run
Runs a one-time task or command in a new container for a specific service.
Command Syntax:
docker-compose run [options] <service_name> <command>
Example:
docker-compose run web python manage.py migrate
Explanation:
Runs the migrate
command inside a new container for the web
service.
8. docker-compose rm
Removes stopped containers for the services defined in the Compose file.
Command Syntax:
docker-compose rm [options]
Common Options:
-f
: Force removal without confirmation.-s
: Remove associated volumes.
Example:
docker-compose rm -f
Explanation:
Deletes all stopped containers without prompting for confirmation.
Monitoring and Debugging
9. docker-compose logs
Retrieves and streams logs from containers in the project.
Command Syntax:
docker-compose logs [options]
Common Options:
-f
: Follows log output in real time.--tail=N
: Displays the last N lines of logs.
Example:
docker-compose logs -f --tail 50
Explanation:
Streams the last 50 lines of logs from all containers in real time.
10. docker-compose exec
Runs commands interactively inside a running container.
Command Syntax:
docker-compose exec [options] <service_name> <command>
Example:
docker-compose exec web /bin/bash
Explanation:
Starts an interactive Bash shell in the web
service container.
11. docker-compose top
Lists the active processes running inside a container.
Command Syntax:
docker-compose top <service_name>
Example:
docker-compose top db
Explanation:
Displays all active processes in the db
service.
Scaling and Configuration
12. docker-compose up --scale
Scales a service to the specified number of container instances.
Command Syntax:
docker-compose up --scale <service_name>=<count>
Example:
docker-compose up --scale web=3
Explanation:
Runs three instances of the web
service.
13. docker-compose config
Validates and displays the current configuration of the docker-compose.yml
file.
Command Syntax:
docker-compose config [options]
Common Options:
--services
: Lists only the services.--volumes
: Lists defined volumes.--resolve-image-digests
: Resolves image digests.
Example:
docker-compose config --services
Explanation:
Validates syntax and lists all services defined in the docker-compose.yml
file.
Cleanup and Environment Management
14. docker-compose down
Stops all running services and removes containers, networks, and volumes.
Command Syntax:
docker-compose down [options]
Common Options:
--volumes
: Removes all associated volumes.--rmi all
: Removes all images built by the project.
Example:
docker-compose down --volumes
Explanation:
Stops all services and deletes associated volumes.
15. docker-compose pull
Pulls updated images for the services in the docker-compose.yml
file.
Command Syntax:
docker-compose pull [options]
Example:
docker-compose pull
Explanation:
- If a service name is specified, only that service's image is pulled.
- If no service name is given, all services' images are updated.
16. docker-compose kill
Forcefully stops running containers immediately.
Command Syntax:
docker-compose kill [service_name]
Example:
docker-compose kill web
Explanation:
- If a service name is specified, only the containers for that service are stopped.
- If no service name is provided, all running containers in the project are stopped forcefully.
17. docker-compose pause / unpause
Temporarily halts or resumes services.
Command Syntax:
docker-compose pause <service_name>
docker-compose unpause <service_name>
Example:
docker-compose pause web
docker-compose unpause web
Explanation:
Pauses or resumes the web
service without stopping it.
Summary of Docker Compose Commands
Below is a list of all the docker-compose
commands covered, along with a one-line explanation for each:
Command |
Explanation |
|
Builds or rebuilds services defined in the Compose file. |
|
Starts and creates containers for services, optionally in detached mode. |
|
Lists the status of running containers in the project. |
|
Starts previously stopped containers without recreating them. |
|
Gracefully stops running containers in the project. |
|
Restarts one or more containers within the project. |
|
Runs a one-off task or command in a new container. |
|
Removes stopped containers for services defined in the Compose file. |
|
Retrieves and streams logs from containers in real-time. |
|
Runs commands interactively inside a running container. |
|
Displays the active processes running inside a container. |
|
Scales a service to run multiple container instances. |
|
Validates and displays the Compose file configuration. |
|
Stops services and removes containers, networks, and volumes. |
|
Downloads the latest versions of service images. |
|
Forcefully stops running containers without waiting for shutdown. |
|
Temporarily pauses services without stopping them. |
|
Resumes services that were paused. |
This summary provides a quick reference to all essential docker-compose
commands, helping you manage multi-container applications efficiently.
FAQ: Docker Compose Commands
What is the purpose of the `docker-compose build` command?
The `docker-compose build` command is used to build or rebuild services defined in the `docker-compose.yml` file, allowing you to create Docker images for your services.
How does the `docker-compose up` command function?
The `docker-compose up` command starts containers for the services defined in the `docker-compose.yml` file, creating them if necessary, and can run them in detached mode.
What does the `docker-compose ps` command do?
The `docker-compose ps` command displays the status of running containers in the project, showing details like container names, IDs, ports, and statuses.
How can you scale services using Docker Compose?
You can scale services using the `docker-compose up --scale` command, which allows you to specify the number of container instances for a service.
What is the function of the `docker-compose down` command?
The `docker-compose down` command stops all running services and removes containers, networks, and volumes associated with the project.