跳转至

Docker Compose 进阶

一句话概述:Docker Compose 是 Docker 官方的多容器编排工具,用一个 YAML 文件定义多个服务,一条命令启动整个应用栈(Web + 数据库 + 缓存)。

核心知识点

概念白话解释
Service服务 = 一个容器的定义(镜像、端口、环境变量等)
Volume卷 = 数据持久化存储(容器删了数据还在)
Network网络 = 容器之间的通信通道
Profile配置文件 = 按需启动部分服务(如只启 dev 相关的)
Depends_on依赖 = 定义服务启动顺序
Healthcheck健康检查 = 确认服务真正准备好了再启动依赖它的服务
Override覆盖 = 用多个 YAML 文件叠加配置
Watch监听 = 文件变化时自动重新构建/同步

安装配置

# Docker Compose 现在是 Docker CLI 的插件(docker compose,不是 docker-compose)
docker compose version  # 查看版本(v2.x)

# 如果没有,安装 Docker Desktop 自带
# 或者单独安装插件
sudo apt install docker-compose-plugin  # Ubuntu

基本使用

1. 完整 Web 应用栈

# docker-compose.yml
services:
  web:  # Web 应用
    build: .  # 从当前目录的 Dockerfile 构建
    ports:
      - "8000:8000"  # 主机8000 → 容器8000
    environment:
      - DATABASE_URL=postgresql://user:pass@db:5432/mydb  # 数据库连接串
      - REDIS_URL=redis://cache:6379  # Redis 连接串
    volumes:
      - ./src:/app/src  # 开发时挂载源码(热更新)
    depends_on:
      db:
        condition: service_healthy  # 等数据库健康检查通过
      cache:
        condition: service_started  # 等缓存启动

  db:  # PostgreSQL 数据库
    image: postgres:16  # 使用 PostgreSQL 16 镜像
    environment:
      POSTGRES_USER: user  # 数据库用户
      POSTGRES_PASSWORD: pass  # 数据库密码
      POSTGRES_DB: mydb  # 数据库名
    volumes:
      - pgdata:/var/lib/postgresql/data  # 数据持久化
    healthcheck:  # 健康检查
      test: ["CMD-SHELL", "pg_isready -U user -d mydb"]  # 检查命令
      interval: 5s  # 每5秒检查
      timeout: 5s  # 超时5秒
      retries: 5  # 重试5次

  cache:  # Redis 缓存
    image: redis:7-alpine  # 轻量 Redis 镜像
    ports:
      - "6379:6379"

volumes:
  pgdata:  # 声明命名卷(数据持久化)
docker compose up -d      # 后台启动所有服务
docker compose ps          # 查看运行状态
docker compose logs -f web # 实时查看 web 服务日志
docker compose down        # 停止并删除容器
docker compose down -v     # 停止并删除容器和卷(数据也删)

2. 生信分析环境

services:
  bioinfo:
    image: biocontainers/samtools:v1.19  # 生信工具镜像
    volumes:
      - ./data:/data  # 挂载数据目录
      - ./results:/results  # 挂载结果目录
    working_dir: /data  # 工作目录
    command: samtools view -b input.sam -o output.bam  # 执行命令

  jupyter:
    image: jupyter/scipy-notebook:latest  # Jupyter 镜像
    ports:
      - "8888:8888"  # Jupyter 端口
    volumes:
      - ./notebooks:/home/jovyan/work  # 挂载笔记本
    environment:
      - JUPYTER_TOKEN=mytoken  # 设置访问密码

高级用法

1. 多环境配置(Override)

# docker-compose.yml(基础配置)
services:
  web:
    image: myapp:latest
    ports: ["8000:8000"]

# docker-compose.override.yml(开发环境,自动加载)
services:
  web:
    build: .  # 开发时从源码构建
    volumes:
      - ./src:/app/src  # 挂载源码
    environment:
      - DEBUG=true  # 开启调试

# docker-compose.prod.yml(生产环境,手动指定)
services:
  web:
    image: myapp:v1.0  # 生产用固定版本
    deploy:
      replicas: 3  # 运行3个实例
    environment:
      - DEBUG=false
# 开发环境(自动加载 override)
docker compose up

# 生产环境(指定文件)
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d

2. Profile(按需启动)

services:
  web:
    image: myapp:latest
    ports: ["8000:8000"]

  debug-tools:  # 调试工具,默认不启动
    image: busybox
    profiles: ["debug"]  # 只有指定 debug profile 才启动
    command: sh

  monitoring:  # 监控,默认不启动
    image: grafana/grafana
    profiles: ["monitoring"]
    ports: ["3000:3000"]
docker compose up  # 只启动 web(没有 profile 的服务)
docker compose --profile debug up  # 启动 web + debug-tools
docker compose --profile debug --profile monitoring up  # 启动全部

3. Watch(开发热更新)

services:
  web:
    build: .
    develop:
      watch:
        - action: sync  # 文件变化时同步到容器
          path: ./src
          target: /app/src
        - action: rebuild  # 配置文件变化时重新构建
          path: ./requirements.txt
docker compose watch  # 启动 watch 模式

4. 自定义网络

services:
  web:
    networks:
      - frontend  # 前端网络
      - backend   # 后端网络

  db:
    networks:
      - backend  # 只在后端网络(web 能访问 db,但外部不能直接访问 db)

networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge
    internal: true  # 内部网络,不能访问外网

常见报错

报错信息原因解决方法
port is already allocated端口被占用换端口或 lsof -i :PORT 找到占用进程
network xxx not found网络不存在docker compose down 后重新 up
volume xxx not found卷不存在docker volume create xxx
depends_on: condition not met依赖服务未就绪检查 healthcheck 配置
build path xxx does not exist构建路径不存在检查 Dockerfile 路径
no configuration file provided找不到 compose 文件确认在项目目录,文件名正确

速查表

# === 生命周期 ===
docker compose up -d          # 后台启动
docker compose down            # 停止并删除
docker compose restart         # 重启
docker compose stop            # 停止(不删除)
docker compose start           # 启动已停止的

# === 查看信息 ===
docker compose ps              # 容器状态
docker compose logs -f [svc]   # 实时日志
docker compose top             # 容器内进程
docker compose config          # 验证并显示最终配置

# === 构建 ===
docker compose build           # 构建所有镜像
docker compose build --no-cache web  # 不用缓存重建 web
docker compose pull            # 拉取所有镜像

# === 执行命令 ===
docker compose exec web bash   # 进入运行中的容器
docker compose run web pytest  # 启动新容器执行命令

# === 扩缩容 ===
docker compose up -d --scale web=3  # 启动3个 web 实例

参考:Docker Compose 文档 | 更新于 2026 年