Skill v1.0.0
currentLLM-judged scan90/100version: "1.0.0" name: docker-compose description: Use when composing multi-container setups — service dependencies and healthchecks, network aliases, variable substitution and .env, exit codes from a one-shot job, and volumes.
Docker Compose
Runs several containers together. For the image itself see dockerfile; for containers started from a test see testcontainers.
Dependencies Are Not Readiness
depends_on alone waits for the container to start, not for the process inside to be ready. That is a race, and it is the usual cause of "works on my machine".
services:dependency:healthcheck:test: ["CMD", "curl", "-sf", "http://localhost:8080/health"]interval: 2stimeout: 3sretries: 15app:depends_on:dependency:condition: service_healthy
For a one-shot container that must finish first, use condition: service_completed_successfully.
Exit Codes Lie by Default
docker compose up --abort-on-container-exit --exit-code-from app
--exit-code-from app reports 0 when `app` never ran at all — for example because its dependency failed a healthcheck. A CI step that trusts it reports green for a run that did not happen.
Read the container's own status instead:
docker compose up --build --abort-on-container-exit --exit-code-from appdocker compose ps -a --format json app | jq -r 'if .State == "exited" then .ExitCode else "did-not-run" end'
Anything other than 0 is a failure, including the container never being created.
Network Aliases
services:mock:networks:default:aliases:- some.host.name
Every container on that network now resolves some.host.name to mock. This is how a client with a compiled-in host is redirected without touching its configuration — see tls-local-mocking, since https needs the certificate to follow.
Aliases work container to container. A process on the host resolves through the host's own resolver and is unaffected.
Variables and .env
Compose substitutes ${VAR} from the environment and from .env in the project directory, before the file is parsed.
environment:OPTIONAL: "${OPTIONAL:-a default}"REQUIRED: "${REQUIRED:?a message naming what to set}"
:? fails the run at parse time with your message. Use it for anything whose absence would otherwise become a confusing error from inside the container.
Quote the value when the message contains a colon. YAML reads KEY: ${VAR:?run: this command} as a nested mapping and fails with mapping values are not allowed in this context, pointing at the line rather than at the colon.
Note the difference between the two ways a variable reaches a container:
${VAR}in the compose file — substituted by Compose, reads.envautomatically.env_file:— passed straight to the container, no substitution and no multi-line values. A PEM does not fit; base64 does.
Multiple Compose Files
docker compose -f other-compose.yml up selects a different file. Useful when one setup is safe and another is not — a mock that cannot reach the real service, versus one that can and will.
Prefer that split over a flag. A file that reaches production is a file worth being hard to run by reflex, and worth keeping out of the repository if the credentials are not there either.
Volumes
volumes:- ./file:/in/container:ro # bind mount, host ownership preserved
A bind-mounted file keeps its host UID and mode. A mode-600 file owned by you is unreadable to a container running as its own non-root user, and the process reports whatever it does when a file is missing — rarely "permission denied".
Always :ro for anything the container should not write.
Review Checklist
depends_onuses a condition, not the bare form.- CI reads the container's exit status, not Compose's.
- Required variables use
:?with a message that names the fix. - Values containing a colon are quoted.
- Bind-mounted files are readable by the container's user.
- A compose that can reach production is separate, and named so nobody runs it by accident.