docker-compose-workflows
Skill1 starsChanged 2 months agoMITView on GitHub
Build real local development environments with Docker Compose - the file structure that scales, named volumes versus bind mounts and the Linux UID permission trap, healthchecks with depends_on service_healthy, profiles, .env handling, networks, and one-shot seed containers. Use when someone needs Postgres plus Redis plus a mail catcher on a laptop, when a dev environment takes minutes to start, or when a container writes files the host cannot read. Also covers when Compose is the wrong tool. Triggers on "docker-compose.yml", "compose file", "local dev environment", "seed data", "profiles", "permission denied bind mount", "depends_on".
- Reads credentials
In this repository126 other files it gives its agents.
- eu-data-backend-job-hunterSkill
- job-application-matcherSkill
- llm-agents-and-tool-useSkill
- llm-application-architectureSkill
- llm-cost-and-latency-optimizationSkill
- llm-evaluationSkill
- llm-fine-tuning-and-adaptersSkill
- llm-privacy-and-safetySkill
- llm-prompt-engineeringSkill
- llm-rag-systemsSkill
- structured-output-and-jsonSkill
- graphql-api-designSkill
- pagination-and-filteringSkill
- rest-api-designSkill
- webhooks-and-integrationsSkill
- caching-architecture-multi-layerSkill
- resilience-patternsSkill
- service-boundaries-and-monolithsSkill
- system-design-interviewSkill
- go-concurrencySkill
- go-error-handlingSkill
- java-spring-patternsSkill
- nodejs-server-performanceSkill
- python-async-patternsSkill
- python-data-modelingSkill
- python-packaging-and-toolingSkill
- python-performanceSkill
- rest-api-implementationSkill
- rust-error-handlingSkill
- rust-ownership-patternsSkill
- typescript-type-safetySkill
- ci-caching-and-speedupSkill
- ci-pipeline-designSkill
- deployment-strategiesSkill
- github-actions-hardeningSkill
- release-versioning-and-changelogsSkill
- reproducible-buildsSkill
- aws-iam-and-securitySkill
- aws-serverless-architectureSkill
- azure-fundamentalsSkill
- cloud-cost-optimizationSkill
- cloud-networking-and-vpcSkill
- gcp-cloud-architectureSkill
- code-review-skillsSkill
- design-review-and-refactoring-architectureSkill
- refactoring-techniqueSkill
- understanding-unknown-codebaseSkill
- buildkit-and-cacheSkill
- container-image-debuggingSkill
- container-runtime-securitySkill
- dockerfile-authoringSkill
- docker-image-optimizationSkill
- database-connection-managementSkill
- database-transactionsSkill
- distributed-databasesSkill
- nosql-data-modelingSkill
- postgres-query-tuningSkill
- redis-and-cachingSkill
- schema-migrationsSkill
- data-modeling-for-analyticsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 259
Every service joins a default network automatically and resolves every other service by service name as a hostname (db, not localhost).
- Very specific: line 364
When a migration breaks, docker compose down -v is the reset — and it destroys data, so gate it.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Docker Compose Workflows
Compose is a local development and CI harness, not an orchestrator. Its whole job is: start these services in the right order, on one network, with the right env, fast, and tear down cleanly.
The file structure that scales
Compose merges in a defined order; later files win. Use one file per concern
and an override file that is not committed.
compose.yaml # base: images, ports, volumes, healthchecks, networks
compose.override.yaml # local-only (gitignored): bind mounts, expose, fast refreshdocker compose up -d # auto-loads compose.override.yaml
docker compose -f compose.yaml up -d # CI / production-shaped: no overrides
docker compose config # merged, resolved, interpolated YAMLdocker compose config is the single most useful command: it shows the
effective config after interpolation, profile handling, and override merging, and
it fails loudly on a typo'd key. Use it in CI.
Base file:
name: myapp # project name; keeps container/volume names stable
x-common: &common # YAML anchor: shared env, reusable without duplication
restart: unless-stopped
logging:
driver: json-file
options: { max-size: "10m", max-file: "3" }
services:
db:
image: postgres:16-alpine
<<: *common
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD:-devpass}
POSTGRES_DB: app
command:
- postgres
- -c
- max_connections=200
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 2s
timeout: 2s
retries: 30
start_period: 10s
volumes:
- pgdata:/var/lib/postgresql/data
networks: [backend]
api:
build:
context: .
target: development # multi-stage: the dev target has hot reload
<<: *common
command: npm run dev
ports: ["3000:3000"]
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD:-devpass}@db:5432/app
NODE_ENV: development
depends_on:
db:
condition: service_healthy
volumes:
- .:/app
- /app/node_modules # anonymous volume: keeps container node_modules
networks: [backend, frontend]
mailcatcher:
image: sj26/mailpit:latest
<<: *common
ports: ["8025:8025", "1025:1025"]
networks: [frontend]
volumes:
pgdata:
networks:
frontend:
backend:
internal: true # no egress to the outside worldOverride file (gitignored):
services:
api:
volumes:
- .:/app
environment:
LOG_LEVEL: debug
db:
ports: ["5432:5432"] # so host tools can connectGotcha in the anchor above: YAML merge keys do not deep-merge. <<: *common
followed by your own volumes: replaces the anchor's volumes entirely; it
does not append. Verified with docker compose config.
Ordering: healthchecks, not sleep
depends_on with the short form only waits for the container to start, not
to be usable. Postgres is listening 2-3 s after "started", and your app will
crash with connection refused on first boot.
depends_on:
db:
condition: service_healthy # waits for the healthcheck to pass
migrate:
condition: service_completed_successfully # v2.20+; for one-shot jobsservice_healthy requires a healthcheck on the target service. Without one,
Compose warns and the dependency is effectively ignored.
Compose v2.20+ adds condition: service_completed_successfully, which is the
correct way to express "run migrations before the app starts":
migrate:
build: { context: ./migrate }
command: ["alembic", "upgrade", "head"]
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD:-devpass}@db:5432/app
depends_on:
db: { condition: service_healthy }
restart: "no"
api:
depends_on:
migrate: { condition: service_completed_successfully }restart: "no" matters — a failing migration that restarts forever hides the
error. Leave the failed container in place so you can docker compose logs
migrate.
Named volumes vs bind mounts
Bind mount - ./src:/app | Named volume - nodemod:/app/node_modules | |
| Who owns the files | host UID | root (or the image's USER) |
| Host sees container writes | yes, immediately | no |
Survives down -v | yes | no (deleted) |
| Good for | source code, live reload | databases, package installs, caches |
The Linux UID trap. Your host user is uid 1000. A container running as
uid 1000 (from USER 1000:1000 in the Dockerfile) writes files owned by 1000 —
fine. A container running as root writes as uid 0, and the bind-mounted host
directory now contains root-owned files you cannot edit or delete without
sudo. Symptom: permission denied on the host immediately after docker
compose up, or EACCES: permission denied, open '/app/node_modules/.cache'
inside the container.
Three fixes, in order of preference:
- Run the container as the host uid. Declare the uid as a build arg so the
image matches whoever is building it:
ARG UID=1000 ARG GID=1000 RUN groupadd -g ${GID} app && useradd -u ${UID} -g ${GID} -m app USER ${UID}:${GID}
Pin the same values indocker compose build --build-arg UID=$(id -u) --build-arg GID=$(id -g)compose.yamlsodocker compose upalone is correct:build: { args: { UID: "${LOCAL_UID:-1000}", GID: "${LOCAL_GID:-1000}" } }. - Named volume for the writable-but-not-source directories (the
node_modulestrick above) so the container never writes to the host tree. - Fix up after the fact — last resort, and it is not idempotent:
sudo chown -R "$(id -u):$(id -g)" ./src.
macOS and Windows Docker Desktop run Linux containers in a VM and present a consistent owner regardless, so this problem is Linux-host-only. Do not spend engineering on it for a mac-only team, and do assume it exists for a Linux CI runner.
.env handling
Compose loads .env from the project directory (next to the compose file,
or the --project-directory value) automatically, and uses it for
${VAR} interpolation in the compose file. It is not automatically injected
into containers — that needs env_file: or explicit environment:.
services:
api:
env_file: [.env] # every key in .env becomes a container env var
environment:
LOG_LEVEL: ${LOG_LEVEL:?set LOG_LEVEL in .env} # ${:?} = hard error if unset${VAR:-default}uses a default;${VAR:?message}fails the whole command if unset. Use${VAR:?…}for anything required in a real environment — a silent empty string is how a service comes up pointed at localhost..envis a file, not a secret store. Commit.env.examplewith the keys and empty values; gitignore.env. Real secrets go in the orchestrator's secret store — seekubernetes-manifestsandcontainer-runtime-security.env_file: .envoverrides nothing by default; if a key is in bothenv_fileandenvironment:,environment:wins.docker compose configprints the interpolated result — that means secrets from.envare printed to your terminal and into CI logs. Do not paste the output ofdocker compose configinto a bug report.
Profiles
Profiles let you keep optional services in the same file without starting them.
services:
db: # no profile: always starts
api: # no profile: always starts
debugger:
image: nicolaka/netshoot
profiles: ["debug", "tools"]
cap_add: [SYS_PTRACE] # needed to attach to other containers
testrunner:
profiles: ["ci"]
command: ["pytest", "-q"]docker compose up -d # db + api only
docker compose --profile debug up -d # + debugger
docker compose --profile debug config --services # verify before startingVerified: a service with profiles: ["debug"] is absent from
docker compose config --services by default and present with
--profile debug. This is the clean way to have one compose file serve
"minimal laptop", "I need to debug", and "CI".
Gotcha: if a profiled service is a dependency of a non-profiled one, it does not get auto-enabled — you must pass the profile explicitly.
Networks
Every service joins a default network automatically and resolves every other
service by service name as a hostname (db, not localhost). That is why
DATABASE_URL=postgres://…@db:5432 works.
networks:
frontend:
backend:
internal: true # no outbound internet: nothing inside can exfiltrateinternal: true on the db network is a genuinely useful default: a database
with an egress path to the internet is a one-command exfiltration. It also
means the db cannot pull packages at runtime, which is what you want anyway.
Rules:
- Split frontend (web-exposed) from backend (data) and put the db only on
backend. Verified default:
docker compose configmaterialises<project>_defaultand attaches every service to it. ports:publishes to the host;expose:documents intra-network access only. Useexposefor anything the host does not need.- Hostname resolution is only for services on a shared network. Two services on disjoint networks cannot resolve each other by name — that is the point of the split, but it surprises people.
- On Linux, the host reaches published ports on
127.0.0.1; on Docker Desktop the same port is on the VM and forwarded. If something works on a Mac and not on a Linux CI runner, check the bind address.
One-shot jobs: seeding, migrations, fixtures
seed:
build: { context: ./seed }
profiles: ["seed"]
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD:-devpass}@db:5432/app
depends_on:
db: { condition: service_healthy }
restart: "no"docker compose --profile seed run --rm seedrun --rm (not up) for a job: it runs once, exits, and removes the container.
restart: "no" prevents a restart loop. --build on run rebuilds the image
first if you changed the seed code. A migration that must run on every up
needs its own version table — that is the difference between "idempotent" and
"works on a fresh database only".
Daily commands
docker compose up -d --wait # wait for healthchecks before returning
docker compose ps # STATUS shows (healthy) / (unhealthy)
docker compose logs -f --tail=100 api
docker compose exec api sh # shell into a running service
docker compose run --rm --entrypoint sh api # shell in a one-off container
docker compose restart api
docker compose up -d --build api # rebuild + recreate
docker compose down # remove containers + networks, KEEP volumes
docker compose down -v # DESTRUCTIVE: also deletes named volumes
docker compose config --services
docker compose config --quiet # syntax/interpolation check for CI--wait (v2.17+) blocks until every service with a healthcheck is healthy or
the timeout expires. Use it in any script; it removes an entire class of flake.
Logs: docker compose logs re-emits multi-line application logs as single
lines, which destroys stack traces. For raw logs, use
docker logs $(docker compose ps -q api).
When Compose is the wrong tool
| Need | Use instead |
| Restart a crashed container on a server you cannot babysit | systemd unit, or a real orchestrator |
| Rolling updates with zero downtime | Kubernetes / ECS / Nomad |
| Horizontal scaling across hosts | anything but Compose |
| Resource limits, quotas, network policy at scale | Kubernetes (kubernetes-manifests) |
| Secrets that must not be in a file on disk | orchestrator secret store / cloud KMS |
| Production hosting | Compose is not a production orchestrator. It has no rescheduling, no self-healing across reboots, and --wait is a convenience, not a health guarantee. |
The honest summary: Compose is excellent for "six services on my laptop, started
in the right order, in 20 seconds" and inadequate for everything else. Do not
present a docker compose up on a single VM as a deployment strategy without
saying that out loud.
Gotchas
version:key is obsolete in v2 and is ignored. Its presence signals an old file; remove it.- The v1/v2 split bites twice:
docker-compose(v1, Python) anddocker compose(v2, plugin) do not have identicalupflags. Use v2 throughout; checkdocker compose version. container_name:breaks scaling and--scaleand forces one project per host. Let Compose name containers<project>_<service>_<n>and address them as<service>on the network.- A bind mount of the whole repo shadows the image's own files. If the image
has
/app/distbuilt in and you mount.:/app, thedistdisappears. Mount subdirectories, or use anonymous volumes for the build output. - Named volumes survive
down, so a "fresh" dev environment is not fresh. When a migration breaks,docker compose down -vis the reset — and it destroys data, so gate it. - Rebuild is not automatic. Editing a
Dockerfileorcompose.yamldoes not change a running container.up -dmay also not rebuild if the image name and tag are unchanged — useup -d --build. - Compose reads
.envfrom the project directory, not the CWD, and the project directory is where the first compose file lives. Runningdocker compose -f path/to/compose.yamlfrom elsewhere can silently interpolate a different.env. Use--project-directoryto pin it. depends_onis not a health dependency by default and, importantly, it does nothing about the app being ready — only about its dependency.- Every service on the default network can reach every other service, including your database's admin port, unless you split networks.
- Healthcheck
CMD-SHELLruns/bin/sh -c— a distroless-based service cannot have one, and thetestmust be a single argument when exec-form.test: ["CMD", "curl", "-f", "http://localhost/"]runs curl directly, with no shell.
Local dev environment checklist
- Every long-running service has a
healthcheckand its dependents usecondition: service_healthy - Jobs (migrate/seed) use
restart: "no"and are invoked withrun --rm - No root-owned files appear in the bind-mounted tree
- The db is on an
internal: truenetwork, not the frontend one .env.examplecommitted,.envignored, and nodocker compose configoutput pasted anywherecompose.override.yamlis gitignoreddocker compose config --quietpasses in CIdocker compose up -d --waitis what CI runs, notsleep 10
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

