What is Docker Compose

September 29, 2021

Introduction

Developing applications using Docker can become challenging when juggling multiple services and containers. Learn about Docker Compose, the tool that will help you run multi-container application environments.

In this tutorial, you will learn all about Docker Compose, the benefits of using this tool, its use cases and features.

What is Docker Compose

What is Docker Compose?

An application can consist of multiple containers running different services. It can be tedious to start and manage containers manually, so Docker created a useful tool that helps speed up the process - Docker Compose.

Docker Compose is software used for defining and running multi-container Docker applications. It can handle multiple containers simultaneously in the production, staging, development, testing, and CI environment. Therefore, use Docker Compose to manage the whole software development lifecycle (SDLC).

Docker Compose works by applying rules defined in a docker-compose.yaml file. The YAML file configures the application's services and includes rules specifying how you want them to run. With the file in place, you can start, stop, or rebuild all the services using a single command. Additionally, you can check the status of a service, display log outputs, and run one-off commands.

Installing Docker Compose is simple. For step-by-step instructions, check out:

Docker Compose Use Cases

Common use cases of Docker Compose include:

  • Automated testing environments. Compose supports automated testing, which is an essential part of CI/CD as it can easily create and destroy the required testing environment. Developers can define and configure the environment needed for running automated end-to-end testing using the appropriate Docker Compose file.
  • Single host deployments. In Docker Compose, containers are designed to run on a single host as they have traditionally been focused on development and testing workflows.
  • Development Environments. Compose is a fast and simple way of starting projects as it can quickly spin up new isolated development environments. The software documents and configures all the application's service dependencies (including databases, caches, web service APIs, etc.). It allows you to create and start one or multiple containers for each dependency using a single command.

Benefits of Docker Compose

Here are some of the main benefits of using Docker Compose:

  • Fast and simple configuration. Thanks to YAML scripts and environment variables, you can easily configure or modify application services.
  • Secure internal communication. Compose creates a network for all the services to share. This adds an extra security layer for the app since the services cannot be accessed externally.
  • Portability and CI/CD support. Since all the services are defined inside the docker-compose file, developers can easily access and share the entire configuration. By pulling the YAML file and source code, they can launch the environment in a matter of minutes. This contributes to setting up and enabling an efficient CI/CD pipeline.
  • Efficient use of resources. Docker Compose allows you to host multiple isolated environments on one host. Running everything on a single piece of hardware lets you save a lot of resources. Its features that enable it to cache a configuration and re-use existing containers also contribute to resource efficiency.
The benefits of using Docker Compose.

Note: Docker Compose supports the CI/CD pipeline which is an important practice for DevOps. If you are searching for the best server solution for your DevOps team, check out Bare Metal Cloud.

BMC is a cloud-native dedicated server that supports IaC tools and simple API, CLI, and SDK interaction.

Docker Compose Features

There are several prominent Docker Compose features that provide the benefits mentioned above.

Hosting Multiple Isolated Environments on a Single Host

Compose keeps environments isolated from one another on a single host using projects names.

By default, the project name is the basename of the project directory, whereas the project directory is the base directory of the docker-compose file. You can change the default values:

  • Set the project name using the -p command line option or the COMPOSE_PROJECT_NAME environment variable.
  • Set the project directory using the --project-directory environment variable.

You can utilize this feature on a dev host to run stable copies of each feature branch of your project by creating multiple copies of the environment under different names.

If you are working on a CI server or a shared host, you want to make sure the builds do not interfere with each other. To do so, set the project names to unique build numbers.

Supporting Environment Variables

You can customize containers for different environments or users by adding environment variables in the docker-compose file. That gives you more flexibility when setting up containers with Compose, as the variable values are not hardcoded in the configuration.

The variable values can be set in the shell environment (from which you run docker-compose) or in a .env file (stored in the project directory). By default, Docker compose applies the values specified in the .env file. However, values set in the shell environment override the ones from the .env file.

You can use this feature for anything from securely providing passwords to specifying a software version.

Preserving Volume Data

Another great feature of Docker Compose is that it saves data used by the services. Therefore, you don't have to worry about losing data created in containers. If there are containers from previous runs, Compose will find them and copy their volumes to the new run.

Re-Using Existing Containers

Compose only recreates containers that have changed since the last run. If there is no change, it re-uses the existing container.

This feature relies on the software's ability to cache container configurations, allowing you to set up your services faster.

Docker Compose Basic Commands

The basic syntax for running Docker Compose commands is:

docker-compose [command]

Note: If you are working as a sudo user, add the sudo prefix before the commands.

Below you will find some of the most basic Docker Compose commands and what they are used for.

CommandDescription
docker-compose --helpshow help, usage instructions for and arguments for the docker-compose command
docker-compose buildlook for all services containing the build: statement in the docker-compose.yml file and run a docker build for each one
docker-compose runrun a one-time command against a service
docker-compose upbuild, (re)create, start, and attach to containers for a service
docker-compose -f [command]specify the location of a docker-compose configuration file by adding the -f flag
docker-compose startstart existing containers for a service
docker-compose stopstop running containers (without removing them)
docker-compose pausepause running containers of a service
docker-compose unpauseunpause paused containers of a service
docker-compose downstop containers (and remove containers, networks, volumes, and images)
docker-compose pslist containers within the docker-compose configuration file
docker-compose imageslist images used by created containers
docker-compose lslist running Compose projects

How to Use Docker Compose?

How to use Docker Compose.

There are three steps to using Compose:

1. The first step is creating a Dockerfile to define the application environment. The file builds Docker images, which will be used as the bases for the containers.

Note: To learn more about creating images, check out How to Create Docker Image with Dockerfile.

2. Next, you need to define the services in a Compose file. Create a new file in the project directory under the name docker-compose.yml and define your services using YAML syntax. An example of such a file would include the following content:

version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/code
      - logvolume01:/var/log
    links:
      - redis
  redis:
    image: redis
volumes:
  logvolume01: {}

3. Finally, run and manage the app using the Docker Compose CLI. You can run a single docker-compose up command to start the entire application in an instance.

Conclusion

After reading this article you should know what Docker Compose is, how it works and what are the benefits of using this handy Docker tool.

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
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
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