Overview
Overview
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s service. Then, with a single command, you create and start all service from your configuration.
Compose works in all environments: production, staging, development, testing, as well as CI workflows.
1. Using Compose
Using compose is basically a three-step process:
- Define your app’s environment with a
Dockerfile
so it can be reproduced anywhere - Define the service that make up your app in
docker-compose.yml
so they can be run together in an isolated environment - Run
docker compose up
and theDocker compose
command starts and runs your entire app
A docker-compose.yml
looks like this :
version: "3.9" # optional since v1.27.0
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
Compose has commands for managing the whole lifecycle of your application:
- Start, stop and rebuild services
- View the status of the running services
- Run a one-off command on a service
2. Multiple isolated environment on a single host
Compose use a project name to isolated environments from each other. You can make use of this project name in several different context:
- on a dev host, to create multiple copies of a single environment, such as when you want to run a stable copy for each feature branch of a project
- on a CI server, to keep build from interfering with each other, you can set the project name to a unique build number
- on a shared host or dev host, to prevent different projects, which may use the same service names, from interfering with each other
The default project name is the basename of the project directory. You can set a custom project name by using -p
command line option or the COMPOSE_PROJECT_NAME
environment variable
The default project directory is the base directory of the Compose file. A custom value for it can be defined with the --procjet-directory
command line option
3. Preserve volume data when containers are created
Compose preserves all volumes used by your services. When docker compose up
runs, if it finds any containers from previous runs, it copies the volumes from the old container to the new container. This process ensures that any data you’ve created in volumes isn’t lost
4. Only recreate containers that have changed
Compose caches the configuration used to create a container. When you restart a service that not changed, Compose re-uses the existing containers. Re-using containers means that you can make changes to your environment very quickly
5. Variables and moving a composition between environments
Compose supports variables in a Compose file. You can use these variables to customize your composition for different environment, of different users
You can extend a Compose file using the extends
filed or by creating multiple Compose files.
6. Common use cases
Compose can be used in many different ways. Some common use cases are outlined below
6.1 Development environment
When you are developing software, the ability to run an isolated environment and interact with it is crucial. The Compose command line tool can be used to create the environment and interact with it
The Compose file provides a way to document and configure all of the application’s dependencies (database, queues, caches, web service APIs, etc.). Using the Compose command line tool you can create and start one or more containers for each dependency with a single command docker compose up
Together, these features provide a convenient way for developers to get started on a project. Compose can reduce a multi-page “developer getting started guide” to a single machine readable Compose file and a few commands
6.2 Automated testing environments
An important part of any Continuous Deployment or Continuous Integration process is the automated test suite. Automated end-to-end testing requires an environment in which to run tests. Compose provides a convenient way to create and destroy isolated testing environments for your testing suite. By defining the full environment in a Compose file, you can create and destroy these environments in just a few commands:
$ docker compose up -d
$ ./run_tests
$ docker compose down