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.
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 -ddocker compose up -d --builddocker compose stopdocker compose downdocker compose restart svcLogs & Status 5
docker compose psdocker compose logs -f svcdocker compose logs --tail=100 svcdocker compose topdocker compose port svc 80Config & Build 5
docker compose configdocker compose builddocker compose pulldocker compose exec svc shdocker compose run --rm svc cmdNetwork & Volume 5
docker compose up -d --scale svc=3docker network lsdocker compose down -vdocker volume lsdocker compose config --volumesFAQ 5
Q: What is the difference between docker compose up and docker compose start?Q: How do I rebuild images?Q: How do I view Compose project logs?Q: How do I start only a specific service?Q: How do I remove all containers and volumes?Parameter matrix
| 参数 | Effect | Example |
|---|---|---|
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先校验配置再启动
docker compose config展开并验证 YAML,避免 up 时因配置错误整体失败。
2查看各服务状态与退出码
docker compose psState 列直接显示 Exit/Restarting,定位问题服务。
3跟踪某服务日志
docker compose logs -f web启动卡住时看该服务的实时输出。
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