🏗️ Docker Compose Notes
Docker compose let’s you start multiple docker images together and is useful when a single application depends on other back end services that are running in other docker containers.
The compose file defines the services, networks, volumes and configuration of the application.
If needed a compose file can have profiles which allow you to further decide what containers to
start/stop. The docker documentation for the docker compose cli is
docs.docker.com/../compose
To better understand profiles, consider the following compose YAML file that defines an application, a database, and an admin and test containers that can be optionally invoked (started).
The sample compose file is:
services:
app:
image: myapp:latest
db:
image: postgres:18.0.3
adminer:
image: adminer:latest
profiles: ["debug"]
tests:
image: my-tests:latest
profiles: ["test"]
The docker compose up command starts services specified in the docker compose file.
By default, docker starts all services with no profile, along with any services
who’s profile is listed in the COMPOSE_PROFILES variable or specified via the command line –profile.
In this sample, application (app) and database (db). The adminer and tests services don’t start.
The command COMPOSE_PROFILES=debug,test docker compose up
is needed to start all of the services in this example. See docker up documentation.
What is a docker compose project?
When you start a docker compose file, a project name is assigned (or you specify it). The project name is used to:
- Create a unique namespace for the containers and networks created by this compose file.
- Prefix the names of the containers, networks, and volumes created by this compose file.
- Allow multiple instances of the same compose file to coexist without conflicts.
Common Docker Compose commands
| Command | Description |
|---|---|
docker compose up -d |
Start services in detached mode |
docker compose down |
Stop and remove resources created by up |
docker compose logs -f |
View logs for all services |
docker compose ps |
List running services |
docker compose build |
Build/rebuild services |
docker compose config --profiles |
List all profiles in the current compose.yml file |
Without compose, you would have to run the necessary docker commands to do the equivalent of what compose gives you
How can I start five (5) containers but have them wait for a base/core service to start first?
To do this you need to use the depends_on configuration with condition: service_healthy.
The following example has five (5) containers but they all must wait for the base container my-base
to start up before they start.
services:
my-base:
image: busybox:latest
command: ["sh", "-c", "sleep 120"]
healthcheck:
test: ["CMD-SHELL", "sleep 10"]
interval: 5s # wait for 5 seconds before starting healthcheck
timeout: 15s # wait for up to 15s for healthcheck to complete before timing out
retries: 1 # Mark container unhealth after 1 retry
Sub2a:
image: busybox:latest
command: ["sh", "-c", "sleep 120"]
depends_on:
my-base:
condition: service_healthy
Sub2b:
image: busybox:latest
command: ["sh", "-c", "sleep 120"]
depends_on:
my-base:
condition: service_healthy
Sub3:
image: busybox:latest
command: ["sh", "-c", "sleep 120"]
depends_on:
my-base:
condition: service_healthy
Sub4:
image: busybox:latest
command: ["sh", "-c", "sleep 120"]
depends_on:
my-base:
condition: service_healthy