Skip to content

Docker Compose

定义 and 运行 multi-容器 applications 使用单个 docker-compose.yml file.

你将学到

  • 什么 Docker Compose 是 and 当到 use 它
  • 编写 docker-compose.yml file
  • Services, networks, and 体积
  • 常见 compose commands

概念讲解

什么是 Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. Instead of running multiple docker 运行 commands with long lists of flags, you describe your entire application — services, networks, volumes — in a single docker-compose.yml file. Then one command, docker compose 上, starts everything together. Compose is perfect for local development environments, CI pipelines, and small production deployments.

The Compose File

A docker-compose.yml file uses YAML syntax. Each service defines a container: which image to use, what ports to map, what volumes to mount, and what environment variables to set. You can also define custom networks for services to communicate and named volumes for persistent data. The file is version-controlled, so your entire development environment is reproducible by anyone on the team.

Services and Dependencies

Compose lets you express dependencies between services with depends_on. If your web app depends on a database, you can ensure the database starts first. You can also define health checks so Compose waits until a service is actually ready to accept connections, not just started. Environment variables can be loaded from .环境 files, and you can override settings with docker-compose.override.yml for local development.

Common Commands

docker compose 上 starts all services (add -d for detached). docker compose 下 stops and removes them. docker compose logs shows all logs. docker compose 构建 rebuilds images. docker compose exec web sh opens a shell in a service. These commands manage your entire application stack as a unit, which is much simpler than managing containers individually.

Compose in Production

While Compose is primarily a development tool, it can be used in production with Docker Swarm (docker stack 部署 -c docker-compose.yml myapp). For larger production deployments, Kubernetes is preferred, but Compose is excellent for small services, CI environments, and local development. Use docker-compose.override.yml for environment-specific changes — the base file defines the common configuration, and the override adds development-only settings like debug ports and volume mounts for hot reloading.

示例代码

              # docker-compose.yml — a web app with a database
services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/app
    depends_on:
      db:
        condition: service_healthy

  db:
    image: postgres:16
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: app
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U user"]
      interval: 5s

volumes:
  pgdata:
            

这个 compose file 定义 web service and 数据库. web service waits for 数据库 health 检查之前开始. pgdata 体积 persists 数据库数据.

动手试一试

常见错误

错误写法

Using depends_on 不使用 health 检查

正确做法

depends_on 仅 waits for 容器到 开始, 不到 是 ready. 添加 healthcheck 所以 Compose waits 直到 service 能 actually 接受连接.

错误写法

不 pinning 镜像 versions in compose files

正确做法

Use 特定 versions 如 postgres:16 相反的 postgres:latest. Unpinned 镜像 lead 到 unpredictable behavior 当新 versions 是 released.

练习 Docker Compose

2 个练习

练习 Docker Compose

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误