Introduction
Managing containerized apps that rely on multiple interconnected services can be error-prone and time-consuming. Docker Compose simplifies application management by providing a declarative way to define services and orchestrate deployment infrastructure.
This article introduces Docker Compose and provides an overview of use cases and benefits.
What Is Docker Compose?
Docker Compose is a tool for running complex Docker applications. It allows users to configure multiple application containers within a single YAML file and then use the file to deploy, scale, and manage the containers simultaneously.
As a CLI tool, Docker Compose uses a set of commands to interact with containers. Currently, there are two CLI implementations of the Docker Compose:
docker-compose
(with a hyphen). An older version coded in Python.docker compose
(with a space). The current implementation written in Go and distributed as a Docker extension.
Note: For step-by-step instructions on installing Docker Compose, read How to Install Docker Compose on Ubuntu.
Docker vs. Docker Compose
Docker is a feature-rich platform for developing, shipping, and running software. It helps users run applications and their dependencies in portable, self-sufficient units called containers.
Docker Compose is a tool within the Docker ecosystem that aims to simplify the management of applications consisting of multiple containers. It provides additional functionality to Docker and relies on Docker for most container management operations.
Note: Learn more about Docker container management.
Docker Compose Use Cases
Common use cases of Docker Compose include:
- Automated testing environments. Compose facilitates automated testing, an essential part of CI/CD, as it can easily create, destroy, and recreate testing environments.
- Single host deployments. Since it focuses on developing and testing workflows, Docker Compose is primarily designed for deployments on the same host.
- Development Environments. Compose is a fast and straightforward way of starting projects as it can quickly spin up new isolated development environments. The software documents and configures all the service dependencies (including databases, caches, web service APIs, etc.). It allows users to create and start one or multiple containers for each dependency using a single command.
Docker Compose Benefits
The main purpose of Docker Compose is to streamline container deployment. The tool achieves this goal by offering the following benefits:
- Fast and simple configuration. YAML scripts and environment variables enable simple configuration of application services.
- Secure internal communication. Compose creates a network for all the services to share. This feature adds an extra security layer for the app since the services are inaccessible externally.
- Portability and CI/CD support. Since all the services are defined inside the YAML file, developers can easily access and share the entire configuration. By pulling the YAML file and source code, you can launch an environment in a few minutes.
- Efficient use of resources. Docker Compose allows users to host multiple isolated environments on one host. Running everything on a single piece of hardware eliminates infrastructure overhead. The ability to reuse existing containers and cache configurations also contributes to resource efficiency.
- Support for DevOps practices. Docker Compose fosters consistency and reproducibility and allows rapid prototyping and iteration. These traits make it a frequent choice of DevOps teams.
Note: If you are looking for the best servers for your DevOps team, check out Bare Metal Cloud. BMC is a cloud-native dedicated server solution that supports IaC tools and simple API, CLI, and SDK interaction.
docker compose Command
The basic syntax for running Docker Compose commands is:
docker compose [command] [options]
For example, to build images specified in the compose.yml file, type the following:
docker compose build
Note: The older Python-based version of Docker Compose featured the docker-compose
command. The new docker compose
syntax aims for backward compatibility with docker-compose
, which means the subcommands and options remain the same.
Below is a list of docker compose
sub-commands and their descriptions:
Command | Description |
---|---|
docker compose build | Looks for the services containing the build: statement in the compose.yml file and run docker build for each. |
docker compose run | Runs a one-off command in a temporary container based on the service's configuration. |
docker compose up | Builds, (re)creates, starts, and attaches to containers defined in the compose.yml file. |
docker compose start | Starts existing containers. |
docker compose stop | Stop running containers (without removing them). |
docker compose pause | Pauses running containers. |
docker compose unpause | Unpauses paused containers. |
docker compose down | Stops and removes containers, their networks, volumes, and images. |
docker compose ps | Lists running containers defined in compose.yml. |
docker compose images | Lists images used by created containers. |
docker compose ls | Lists running Docker Compose projects. |
docker compose Options
Aside from subcommands, the docker compose
command also accepts options. Find the comprehensive list of options in the table below:
Option | Description |
---|---|
--all-resources | Includes all resources, including those not used by services. |
--ansi [never|always|auto] | Controls when to print ANSI control characters. The possible values are never, always, and auto (default). |
--compatibility | Runs Docker Compose in a backward compatibility mode. |
--dry-run | Executes a command in dry run mode. |
--env-file [path] | Specifies an alternate environment file. |
-f [path] | Specifies the location of a docker-compose configuration file. |
--help | Shows help, usage instructions, and arguments for the docker compose command. |
--parallel [number] | Controls maximum parallelism. The default value is -1 (unlimited). |
--profile [profile] | Specifies a profile to enable. |
--progress [tty|plain|json|quiet|auto] | Sets progress output type. The possible values are tty, plain, json, quiet, and auto (default). |
--project-directory [path] | Specifies an alternate working directory. The default value is the path of the compose.yml file. |
-p , --project-name [name] | Gives a name to the project. |
How to Create Docker Compose File
Creating a Docker Compose YAML involves generating the file and configuring the necessary services. Follow the steps below to create a Docker Compose file:
1. Generate the file using a text editor like Nano, preferably in the main project directory:
nano compose.yml
The recommended name for the file is compose.yml. It is the filename Compose looks for unless a custom name is provided.
2. Define the services using YAML syntax. For example, the following file defines a three-container WordPress deployment, which consists of WordPress, MariaDB, and Nginx containers:
services:
wordpress:
image: wordpress:latest
ports:
- "8080:80"
depends_on:
- database
environment:
WORDPRESS_DB_HOST: database
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
volumes:
- ./wordpress_data:/var/www/html
database:
image: mariadb:latest
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- ./database_data:/var/lib/mysql
nginx:
image: nginx:latest
ports:
- "80:80"
depends_on:
- wordpress
# Configure nginx to proxy requests to the WordPress container
# This requires an nginx configuration file within the nginx container
volumes:
wordpress_data:
database_data:
The services section is the core section of a compose.yml file. It defines a list of services that make up an application. Each service must have at least one key, such as image or build.
The optional parts of the file include:
- version. Specifies the Compose file format. Defaults to 3.9.
- networks. Defines custom service networks.
- volumes. Declares volumes that can be shared between services or persisted across container restarts.
- configs. Defines configuration files for mounting into containers.
- secrets. Defines secrets for passing information securely.
3. Save the file and exit.
How to Run Docker Compose File
With compose.yml ready in the project directory, run the docker compose up
command to start the entire application at once:
docker compose up
To continue to use the current shell prompt, run the command in the detached mode:
docker compose up -d
Conclusion
This article overviewed Docker Compose, explained how the tool works, and listed the standard benefits. It also introduced the compose.yml file and the frequently used subcommands.
Next, learn how to set up letsencrypt on an Nginx server running on Docker using a Docker Compose configuration file.