What Is Docker Compose: Definition, Usage, Benefits

December 26, 2024

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: Definition, Usage, 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
The output of the docker compose build command.

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:

CommandDescription
docker compose buildLooks for the services containing the build: statement in the compose.yml file and run docker build for each.
docker compose runRuns a one-off command in a temporary container based on the service's configuration.
docker compose upBuilds, (re)creates, starts, and attaches to containers defined in the compose.yml file.
docker compose startStarts existing containers.
docker compose stopStop running containers (without removing them).
docker compose pausePauses running containers.
docker compose unpauseUnpauses paused containers.
docker compose downStops and removes containers, their networks, volumes, and images.
docker compose psLists running containers defined in compose.yml.
docker compose imagesLists images used by created containers.
docker compose lsLists 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:

OptionDescription
--all-resourcesIncludes 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).
--compatibilityRuns Docker Compose in a backward compatibility mode.
--dry-runExecutes 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.
--helpShows 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
The output of the docker compose up command.

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.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
What is Docker?
January 17, 2024

Docker is a popular container-based platform. Learn all about Docker, its components and how it works.
Read more
Kubernetes vs. Docker: Differences and Similarities Explained
October 13, 2022

Learn what the difference is between Docker and Kubernetes. What are these two container technologies...
Read more
10 Docker Security Best Practices
September 14, 2020

This article provides 10 container security tips that can help you prevent attacks and privilege breaches.
Read more
Docker Volumes: How to Create & Get Started
July 27, 2020

Persist data in Docker containers by mounting Docker volumes to containers. You can use an existing...
Read more