Docker Compose Cheatsheet - Command Reference

For developers who need to bring up, stop, and upgrade a whole stack at once — database, cache, backend, frontend — as a single unit. Compose value is not in the commands themselves but in a single docker-compose.yml that pins down the network, dependencies, and config between services so the whole environment can be started and stopped repeatedly. By the end you can bring the stack up with up, find the failing service with logs, clean up fully (and safely) with down -v, and scale out with --scale.

Containers & Orchestration·25 commands·Last updated 2026-07-21
dockercomposeOrchestrationMulti-Container

Typical Use Case

Orchestrate multi-container apps with one YAML: bring up/down a whole stack, manage networks and shared volumes, view aggregated logs, scale services, and troubleshoot dependency order, port conflicts and failed health checks.

Start & Stop 5

docker compose up -d
Start all services in background, omit -d for foreground logs
docker compose up -d --build
Force rebuild images before starting
docker compose stop
Stop services but keep containers for quick restart
docker compose down
Stop and remove containers and networks, add -v for volumes
docker compose restart svc
Restart a specific service

Logs & Status 5

docker compose ps
List service status in the project
docker compose logs -f svc
Follow logs for a service, omit svc for all
docker compose logs --tail=100 svc
View last 100 lines for a service
docker compose top
View running processes inside service containers
docker compose port svc 80
View actual host port mapped to service port

Config & Build 5

docker compose config
Validate and expand compose config, see variable substitution
docker compose build
Build all service images, add --no-cache to bypass cache
docker compose pull
Pull all service images, do this before updating
docker compose exec svc sh
Shell into a running service container
docker compose run --rm svc cmd
Run a one-off command in a service container, --rm removes it after

Network & Volume 5

docker compose up -d --scale svc=3
Horizontally scale a service to 3 instances
docker network ls
List networks, compose projects create their own
docker compose down -v
Remove project and clean up named volumes, data will be lost
docker volume ls
List volumes, compose volume format is project_name_volume_name
docker compose config --volumes
List all volumes defined in the compose file

FAQ 5

Q: What is the difference between docker compose up and docker compose start?
A: docker compose up creates and starts containers (add -d to run in background); docker compose start only starts existing stopped containers without recreating them.
Q: How do I rebuild images?
A: docker compose build rebuilds all service images; add --no-cache to force a full rebuild. docker compose up -d --build builds automatically before starting.
Q: How do I view Compose project logs?
A: docker compose logs -f shows all services; docker compose logs -f <service> shows only one service; add --tail=100 to limit lines.
Q: How do I start only a specific service?
A: docker compose up -d <service> starts only that service and its dependencies, leaving others stopped.
Q: How do I remove all containers and volumes?
A: docker compose down -v removes all containers and networks and cleans named volumes; data is permanently lost, use with caution in production.

Parameter matrix

参数EffectExample
up -d后台创建并启动所有服务docker compose up -d
down停止并移除容器、网络(默认保留卷)docker compose down
ps查看各服务运行状态docker compose ps
logs查看聚合日志docker compose logs -f web
exec在运行中服务内执行命令docker compose exec web sh
--build启动前重新构建镜像docker compose up -d --build
scale / up --scale扩缩指定服务实例数docker compose up -d --scale web=3
restart重启服务docker compose restart web
config校验并展开最终配置docker compose config
pull仅拉取服务所用镜像docker compose pull

Common pitfalls

Symptomup 后某个服务反复重启或一直 starting。

Cause依赖服务未就绪(如 web 依赖 db 但无健康检查/depend_on 只等启动不等待就绪),或端口冲突。

Fix为依赖服务加 healthcheck,用 depends_on 的 condition: service_healthy;用 docker compose logs 看具体报错。

Symptom容器间用服务名连不通。

Cause服务不在同一自定义网络,或仍用 localhost 指向自身而非服务名。

FixCompose 默认建网络,用服务名(如 db)作主机名;应用连接串写服务名而非 127.0.0.1。

Symptom修改了 docker-compose.yml 但 up 没生效。

Cause直接改文件未加 --build,或旧容器未重建。

Fix改了构建相关配置后用 docker compose up -d --build;纯运行参数改动先 down 再 up。

Symptomdown 后数据没了。

Cause数据写在容器层而非命名卷,down 默认删除容器。

Fix在 compose 中用 named volume 持久化数据;down 默认保留卷,必要时才加 -v 清理。

Symptom多个项目端口/网络名冲突。

CauseCompose 默认项目名相同导致资源命名冲突。

Fix用 -p 或 COMPOSE_PROJECT_NAME 区分项目;或在 compose 中显式命名网络/卷。

Symptom构建缓存导致代码改动没进镜像。

CauseCOPY 放在依赖安装之前,或 .dockerignore 漏配导致缓存命中。

Fix把不常变的层放前面;用 --build 强制重建;检查 .dockerignore 是否排除了应包含的目录。

Troubleshooting

  1. 1先校验配置再启动

    docker compose config

    展开并验证 YAML,避免 up 时因配置错误整体失败。

  2. 2查看各服务状态与退出码

    docker compose ps

    State 列直接显示 Exit/Restarting,定位问题服务。

  3. 3跟踪某服务日志

    docker compose logs -f web

    启动卡住时看该服务的实时输出。

  4. 4在运行中的服务内排查

    docker compose exec web sh

    进容器验证网络连通、文件与进程状态。

Tips

  • Compose commands look for docker-compose.yml in the current directory by default; use -f for other files, -p for project name.
  • down does not remove volumes by default — add -v to remove them; back up production volumes first, named volume data is irrecoverable.
  • Services scaled with --scale must not use fixed host ports, or multiple instances will conflict.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us