A quick reference for writing compose.yaml with the current
Compose Specification.
This sheet covers Compose files, not Docker Engine commands or Dockerfile instructions.
Unless a snippet shows a top-level key such as services, place it inside a service definition.
Newer features require a compatible Docker Compose version.
Compose File
|
compose.yaml name: demo services: web: image: nginx:stable ports: - "127.0.0.1:8080:80"
|
|
|
name name: demo Top-level project name. Used to scope generated resource names.
|
|
|
version Omit the top-level |
Service Basics
|
image image: nginx:stable Image name, optionally with a registry, tag, or |
|
|
command command: ["redis-server", "--appendonly", "yes"] Overrides the image's default command. String form does not automatically run in a shell. Request one explicitly for shell expansion: command: ["/bin/sh", "-c", "echo $$HOSTNAME"]
|
|
|
entrypoint entrypoint: ["/app/entrypoint.sh"] command: ["serve"] Overrides the image's entrypoint. A non-null entrypoint also discards the
image's default command; set |
|
|
working_dir, user working_dir: /app user: "1000:1000" Override the working directory and user, optionally with a group. Mounted paths must have suitable permissions for this user. |
|
|
init: true stop_signal: SIGTERM stop_grace_period: 30s
|
|
|
labels labels: com.example.description: "Web application" Container metadata, as a map or a list of |
|
|
container_name Sets a fixed container name. Usually omit it and let Compose generate names.
A service with |
Environment and Interpolation
|
environment environment: APP_ENV: production DEBUG: "false" PORT: "8080" Sets variables inside the container. Map and |
|
|
env_file env_file: - ./default.env - ./local.env Reads container environment variables from files.
Later files override earlier ones. Relative paths use the Compose file's directory.
Unlike Compose 2.24.0+ supports optional files: env_file: - path: ./local.env required: false |
|
|
${VAR}, defaults, required values image: "nginx:${TAG:-stable}" environment: API_URL: "${API_URL:?Set API_URL}"
Host shell variables take precedence over interpolation values from |
|
|
$$ command: ["/bin/sh", "-c", "echo $$HOSTNAME"] Escapes a dollar sign so Compose does not interpolate it.
Here the container's shell expands |
Startup and Health
|
depends_on depends_on: - db Starts dependencies first and removes them last. Short syntax waits for startup, not application readiness. Use long syntax to wait for health or successful completion: depends_on: db: condition: service_healthy migrate: condition: service_completed_successfully
|
|
|
healthcheck healthcheck: test: ["CMD", "pg_isready", "-U", "postgres"] interval: 10s timeout: 5s retries: 5 start_period: 30s Runs inside the container, so the check command must exist in the image.
|
|
|
restart restart: unless-stopped
A failed healthcheck alone does not restart a container.
This policy differs from |
Networking
|
ports ports: - "127.0.0.1:8080:80" - "127.0.0.1:5353:53/udp" Short syntax: Long syntax: ports: - target: 80 published: "8080" host_ip: 127.0.0.1 protocol: tcp |
|
|
expose expose: - "8080" Declares container ports without publishing them to the host.
Services on a shared network can communicate without |
|
|
networks, aliases services: api: image: example/api:1.0 networks: backend: aliases: [app] db: image: postgres:17 networks: [backend] networks: backend: internal: true Services without explicit networks join the project's default network.
On a shared network, service names and network-scoped aliases resolve through DNS.
Inside |
|
|
networks.external networks: shared: external: true name: shared-network Top-level definition of an existing network. Attach services with |
|
|
network_mode network_mode: "service:vpn" Shares another service's network namespace. Other modes include |
Storage and Files
|
volumes services: db: image: postgres:17 volumes: - db-data:/var/lib/postgresql/data volumes: db-data: {} Short syntax: |
|
|
volumes: bind mounts volumes: - type: bind source: ./config target: /etc/app read_only: true bind: create_host_path: false Long syntax distinguishes |
|
|
volumes.external, volumes.name volumes: db-data: external: true name: existing-db-data Top-level definition of an existing volume; Compose does not create or delete it.
|
|
|
secrets services: db: image: postgres:17 environment: POSTGRES_PASSWORD_FILE: /run/secrets/db_password secrets: - db_password secrets: db_password: file: ./secrets/db_password.txt Declare secrets at the top level, then grant each service access explicitly.
Short syntax mounts a secret at |
|
|
configs services: web: image: nginx:stable configs: - source: nginx_config target: /etc/nginx/nginx.conf configs: nginx_config: file: ./nginx.conf Mount non-secret configuration files without rebuilding the image. Defining a config does not grant service access; list it on the service. |
Building Images
|
build.context, build.dockerfile, build.target build: context: . dockerfile: docker/Dockerfile target: production
|
|
|
build.args, build.secrets build: context: . args: APP_VERSION: "1.0" secrets: - build_token
|
|
|
pull_policy image: example/app:dev build: . pull_policy: build Controls pulling versus building. Common values: |
Runtime Settings
|
cpus: 1.5 mem_limit: 512m pids_limit: 100 Limit CPU allocation, memory, and process count.
Support depends on the runtime and platform.
|
|
|
read_only, tmpfs read_only: true tmpfs: - /tmp Makes the root filesystem read-only while allowing temporary writes in |
|
|
cap_drop: - ALL security_opt: - no-new-privileges:true Linux example: drop capabilities and prevent privilege escalation through
mechanisms such as setuid binaries. Add only capabilities the application needs.
Avoid |
|
|
logging logging: driver: json-file options: max-size: "10m" max-file: "3" Select a logging driver and its options. Options are strings and depend on the driver. This example limits log size and retains three files. |
Reuse and Overrides
|
profiles profiles: [debug] Enable this service with |
|
|
x- extensions, anchors x-common: &common restart: unless-stopped init: true services: web: <<: *common image: nginx:stable worker: <<: *common image: example/worker:1.0 Compose ignores |
|
|
include include: - ./database/compose.yaml Top-level inclusion of another Compose model (Compose 2.20.0+).
Included files resolve relative paths from their own project directories.
Do not rely on |
|
|
merge, !reset, !override Combine files with Override-file fragment: services: web: ports: !override - "127.0.0.1:8443:443" environment: DEBUG: !reset null
|
Validation
|
docker compose config docker compose config --quiet docker compose config docker compose config --environment Validate the model, print its resolved form, or inspect interpolation inputs. These commands do not start containers. Resolved output can contain credentials: do not publish it unredacted. |