Docker Compose Cheat Sheet
Docker Compose defines a multi-container application (its services, networks and volumes) in one YAML file and runs it with one command. A service is one component (web app, database, proxy) backed by one or more containers that can be replaced or scaled independently.
Overview
| Behaviour | Why it matters |
|---|---|
| One isolated environment per project name | several copies of a stack can run on one host |
| Volumes survive container recreation | data isn't lost on up after a change |
| Only changed services are recreated | up -d after an edit is quick |
| Variables and override files | the same composition moves between environments |
Rule of thumb: one process (one application) per container.
Details
Everyday Commands
docker compose build # build images
docker compose up -d # start in the background
docker compose up -d --build # rebuild changed images, then start
docker compose down # stop and remove containers and networks
docker compose logs -f web # follow one service's logs
docker compose exec web sh # shell in a running service
docker compose exec --user root web sh # as root, for debugging only
Another File, No Cache
docker compose -f docker-compose-prod.yml build --no-cache
docker compose -f docker-compose-prod.yml up -d
Redeploy One Service
git pull
docker compose -f docker-compose-prod.yml up -d --build --no-deps web # rebuild and replace only web
docker compose -f docker-compose-prod.yml logs --tail=50 -f web
build alone only produces a new image: the running container keeps the old one until up recreates it (see Rebuilding an Image Does Not Update the Container Already Running It). --no-deps leaves the database and the other services running. For a terminal dashboard of containers, logs and resource use, there is lazydocker (brew install jesseduffield/lazydocker/lazydocker).
Volumes and Cleanup
docker volume ls
docker images # then: docker rmi <image>
docker system prune -a # unused containers, networks, all unused images, build cache
docker system prune -a --volumes # ...and unused volumes
Full Clean Rebuild
docker compose -f docker-compose-prod.yml down -v
docker system prune -a
docker compose -f docker-compose-prod.yml up -d --build
docker ps -a && docker volume ls # check nothing from the old stack remains
References
- Docker documentation: Compose file reference - services, volumes, networks.
- Docker documentation: docker compose CLI - every subcommand.
- Docker documentation: docker system prune - what each flag removes.
- Docker for Beginners (TestDriven.io) and Docker Best Practices (TestDriven.io) - the articles I learned from.
- Docker and Container Security MOC - hub for Docker notes.
Review cards
Flashcard You changed one service's image tag in the compose file. What does `docker compose up -d` recreate?
Only that service: Compose compares each service's configuration with its running container and leaves the unchanged ones alone.
Flashcard Why add `--no-deps` to `docker compose up -d --build web`?
Without it, Compose also starts the services web depends on, such as the database, and may recreate them. With it, only web is touched.
Flashcard What does docker compose down -v delete that plain down keeps?
The stack's named volumes: the database, uploaded media, everything. Use it only when you want an empty state, and back up first on a server.
Quiz Which command removes unused containers, networks, all unused images and the build cache, but keeps volumes?
- docker system prune -a
- docker system prune -a --volumes
- docker volume prune
- docker compose down -v
Volumes are only pruned when you add --volumes.