docker-compose

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Docker Compose Configuration Skill

Docker Compose配置技能

Overview

概述

This skill generates Docker Compose configurations for multi-container applications. It creates properly structured compose files with:
  • Service definitions
  • Network configuration
  • Volume management
  • Health checks
  • Dependencies
  • Environment configuration
本技能为多容器应用生成Docker Compose配置。它创建结构规范的compose文件,包含:
  • 服务定义
  • 网络配置
  • 卷管理
  • 健康检查
  • 依赖关系
  • 环境配置

Activation

激活场景

Use this skill when:
  • Creating a new compose file
  • Adding services to existing compose
  • Configuring container orchestration
  • Setting up development or production environments
在以下场景使用本技能:
  • 创建新的compose文件
  • 为现有compose添加服务
  • 配置容器编排
  • 设置开发或生产环境

Process

操作流程

1. Understand Requirements

1. 理解需求

Gather information about:
  • Required services (web, api, database, cache, etc.)
  • Environment (development/production)
  • Networking needs (internal, external, SSL)
  • Persistence requirements
  • Scaling needs
收集以下信息:
  • 所需服务(web、api、数据库、缓存等)
  • 环境(开发/生产)
  • 网络需求(内部、外部、SSL)
  • 持久化要求
  • 扩容需求

2. Consult Documentation

2. 查阅文档

Read relevant documentation:
  • 03-compose-fundamentals.md
    for structure
  • 04-networking.md
    for networks
  • 05-databases.md
    for database services
  • 06-services.md
    for dependencies
  • 08-volumes.md
    for persistence
阅读相关文档:
  • 03-compose-fundamentals.md
    了解结构
  • 04-networking.md
    了解网络配置
  • 05-databases.md
    了解数据库服务
  • 06-services.md
    了解依赖关系
  • 08-volumes.md
    了解持久化配置

3. Generate Configuration

3. 生成配置

Create compose file following best practices:
yaml
undefined
遵循最佳实践创建compose文件:
yaml
undefined

Modern compose (no version field)

Modern compose (no version field)

services: app: build: context: . dockerfile: Dockerfile restart: unless-stopped ports: - "3000:3000" environment: - NODE_ENV=production depends_on: db: condition: service_healthy networks: - frontend - backend healthcheck: test: ["CMD", "curl", "-f", "http://localhost:3000/health"] interval: 30s timeout: 10s retries: 3
db: image: postgres:16-alpine restart: unless-stopped environment: POSTGRES_USER: ${DB_USER} POSTGRES_PASSWORD: ${DB_PASSWORD} POSTGRES_DB: ${DB_NAME} volumes: - postgres_data:/var/lib/postgresql/data networks: - backend healthcheck: test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"] interval: 10s timeout: 5s retries: 5
volumes: postgres_data:
networks: frontend: backend: internal: true
undefined
services: app: build: context: . dockerfile: Dockerfile restart: unless-stopped ports: - "3000:3000" environment: - NODE_ENV=production depends_on: db: condition: service_healthy networks: - frontend - backend healthcheck: test: ["CMD", "curl", "-f", "http://localhost:3000/health"] interval: 30s timeout: 10s retries: 3
db: image: postgres:16-alpine restart: unless-stopped environment: POSTGRES_USER: ${DB_USER} POSTGRES_PASSWORD: ${DB_PASSWORD} POSTGRES_DB: ${DB_NAME} volumes: - postgres_data:/var/lib/postgresql/data networks: - backend healthcheck: test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"] interval: 10s timeout: 5s retries: 5
volumes: postgres_data:
networks: frontend: backend: internal: true
undefined

Service Patterns

服务模式

Web Application

Web应用

yaml
services:
  web:
    build: .
    ports:
      - "80:80"
    depends_on:
      - api
yaml
services:
  web:
    build: .
    ports:
      - "80:80"
    depends_on:
      - api

API Service

API服务

yaml
services:
  api:
    build: ./api
    expose:
      - "3000"
    environment:
      - DATABASE_URL=postgresql://user:pass@db:5432/app
    depends_on:
      db:
        condition: service_healthy
yaml
services:
  api:
    build: ./api
    expose:
      - "3000"
    environment:
      - DATABASE_URL=postgresql://user:pass@db:5432/app
    depends_on:
      db:
        condition: service_healthy

Database Service

数据库服务

yaml
services:
  db:
    image: postgres:16-alpine
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      retries: 5
yaml
services:
  db:
    image: postgres:16-alpine
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready"]
      interval: 10s
      retries: 5

Cache Service

缓存服务

yaml
services:
  redis:
    image: redis:7-alpine
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
yaml
services:
  redis:
    image: redis:7-alpine
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data

Background Worker

后台工作进程

yaml
services:
  worker:
    build: .
    command: npm run worker
    depends_on:
      - db
      - redis
yaml
services:
  worker:
    build: .
    command: npm run worker
    depends_on:
      - db
      - redis

Network Patterns

网络模式

Internal Database Network

内部数据库网络

yaml
networks:
  backend:
    internal: true  # No external access
yaml
networks:
  backend:
    internal: true  # No external access

External Shared Network

外部共享网络

yaml
networks:
  proxy:
    external: true
    name: traefik_proxy
yaml
networks:
  proxy:
    external: true
    name: traefik_proxy

Volume Patterns

卷模式

Named Volume

命名卷

yaml
volumes:
  postgres_data:
yaml
volumes:
  postgres_data:

Bind Mount (Development)

绑定挂载(开发环境)

yaml
volumes:
  - ./src:/app/src
yaml
volumes:
  - ./src:/app/src

Anonymous Volume

匿名卷

yaml
volumes:
  - /app/node_modules
yaml
volumes:
  - /app/node_modules

Environment Patterns

环境变量模式

Direct Values

直接赋值

yaml
environment:
  - NODE_ENV=production
yaml
environment:
  - NODE_ENV=production

From .env File

来自.env文件

yaml
env_file:
  - .env
yaml
env_file:
  - .env

With Defaults

带默认值

yaml
environment:
  - DB_HOST=${DB_HOST:-localhost}
yaml
environment:
  - DB_HOST=${DB_HOST:-localhost}

Output

输出结果

Generated compose file includes:
  • All required services
  • Proper network configuration
  • Volume definitions
  • Health checks
  • Dependency management
  • Environment configuration
  • Comments for complex settings
生成的compose文件包含:
  • 所有所需服务
  • 规范的网络配置
  • 卷定义
  • 健康检查
  • 依赖管理
  • 环境配置
  • 复杂配置的注释说明