How to use docker compose

Docker Compose is a tool that makes it easier to manage and run multi-container applications using Docker. It lets you define all the services your application needs in a single YAML file, and then start them all up with just one command. Let's walk through how to use Docker Compose and some of its key features.
Getting Started with Docker Compose
To use Docker Compose, you'll need a docker-compose.yml
or compose.yml
file. This file lists all the services (containers) your application needs. For example, if you're building a web application, you might have services for a web server, a database, and a cache.
Here's how you can use Docker Compose to manage your application:
Basic Usage
To start your application, navigate to the directory containing your Compose file and run:
docker compose up
This command reads the docker-compose.yml
or compose.yml
file in your current directory and starts all the services defined within it.
Specifying Different Compose Files
Sometimes, you might want to use different Compose files for different environments or configurations. You can specify which file to use with the -f
flag. For instance, if you have a file named docker-compose.prod.yml
for your production environment, you can start it with:
docker compose -f docker-compose.prod.yml up
You can also use multiple Compose files to layer configurations. If you have a base configuration and an override file, you can combine them like this:
docker compose -f docker-compose.yml -f docker-compose.override.yml up
In this case, settings in docker-compose.override.yml
will override or add to those in docker-compose.yml
.
Setting a Project Name
Docker Compose uses project names to group related containers together. You can set a custom project name with the -p
flag. For example:
docker compose -p myproject up
This is helpful if you're running multiple Docker Compose projects on the same system and want to keep them organized.
Using Profiles for Optional Services
Profiles let you enable or disable optional services. If you have services that you only need sometimes, you can tag them with a profile in your Compose file. Then, you can enable these services when starting your application. For example:
docker compose --profile debug up
This command starts your application with the debug
profile enabled, which might include additional debugging tools or services.
Configuring Parallelism
When pulling images or building services, Docker Compose can do these tasks in parallel to speed things up. You can control the level of parallelism with the --parallel
flag. For example, to limit parallelism to one task at a time:
docker compose --parallel 1 pull
Setting Environment Variables
You can use environment variables to customize how Docker Compose runs. For instance, to specify a different Compose file without using the -f
flag, you can set the COMPOSE_FILE
environment variable:
export COMPOSE_FILE=docker-compose.prod.yml
docker compose up
Similar environment variables exist for project names (COMPOSE_PROJECT_NAME
), profiles (COMPOSE_PROFILES
), and parallelism (COMPOSE_PARALLEL_LIMIT
).
Testing Commands with Dry Run
Before making changes to your application, you can test commands with the --dry-run
flag. This shows you what Docker Compose would do without actually doing it. For example:
docker compose --dry-run up
This command will list all the steps Docker Compose would take to start your application, but it won't actually start anything.
Common Docker Compose Commands
Here are some common commands you'll use with Docker Compose:
docker compose build
: Builds or rebuilds services.docker compose up
: Creates and starts containers.docker compose down
: Stops and removes containers and networks.docker compose logs
: Views output from containers.docker compose ps
: Lists containers.docker compose exec
: Executes a command in a running container.
Each of these commands can be tailored with the options and flags we've discussed, like -f
, -p
, --profile
, and --parallel
, to fit your specific needs.
By using Docker Compose, you can simplify the management of your multi-container Docker applications, making it easier to develop, test, and deploy your projects.