Docker Compose

用一个 compose.yaml 描述多容器应用,一条命令起停整套环境。

Beginner
Key PointsCompose 是单机多容器编排:一份 YAML 定义服务、网络、卷,up 一起起、down 一起清。

一份 compose.yaml

YAML
services:
  api:
    build: .
    ports:
      - "8080:8080"
    environment:
      DB_HOST: db
      DB_USER: app
      DB_PASSWORD: secret
    depends_on:
      db:
        condition: service_healthy
    restart: unless-stopped

  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: app
      MYSQL_USER: app
      MYSQL_PASSWORD: secret
    volumes:
      - db-data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "-uroot", "-proot"]
      interval: 5s
      timeout: 3s
      retries: 20

volumes:
  db-data:

常用命令

Bash
docker compose up -d          # 后台起全部服务
docker compose ps
docker compose logs -f api
docker compose exec api sh
docker compose restart api
docker compose down           # 停并删容器、网络
docker compose down -v        # 连卷一起删(数据没了)

关键字段

FieldMeaning
buildBuild from a Dockerfile in this dir
imageUse an existing image
depends_onStart order; with condition waits for health
volumesNamed volume or bind mount
environment / env_fileEnv vars inline or from a file
restartno / always / on-failure / unless-stopped
profilesOnly start with --profile x

开发时的热更新

YAML
services:
  api:
    build:
      context: .
      target: dev
    volumes:
      - .:/app              # 源码挂进容器
      - /app/node_modules   # 用镜像里的依赖,避免覆盖
    command: npm run dev

The second line is an anonymous volume: it masks the container's node_modules so a host directory cannot overwrite Linux-built native modules.

depends_on only orders startup, it does not wait for readiness. Without healthcheck + condition: service_healthy (or retries in the app), the API will still hit a database that is not ready.

多环境

Bash
# compose.override.yaml 会自动叠加(开发用)
docker compose up -d

# 指定多个文件,后面的覆盖前面的
docker compose -f compose.yaml -f compose.prod.yaml up -d

# 用 --env-file 换一套变量
docker compose --env-file .env.prod up -d

Friends & Partners