跳到主内容
智客 ZICQ

技能库 智客分类:运维与云 docker-compose

Docker Compose

人们为人而活,为人而活,为人而活.

105 安装量

官方网址:skills.sh

技能介绍

先看中文介绍;官方 description 原文单独保留,不改写 SKILL.md。

做什么

Docker Compose的专家指导——为多容器应用程序写作生产级作曲.yml文件. 覆盖完成 Compose 文件语法( 服务, 网络, 卷, 机密, 配置, 扩展, 包括), 服务配置( 图像/ 构建, 端口, 环境, env_ file, 依赖健康检查, 部署资源, 重新启动政策), 编成网络( 默认网络, 自定义网络, 内部网络) , 量管理, 环境可变策略( envs env_ file), 覆盖多环境文件, 配置可选服务, 并用 kombose 迁移到 Kubernetes

何时用

** 用户提到时总是使用这种技能:**

代理如何加载

按 Agent Skills 渐进披露:启动时只加载 name 与 description(约 100 token);任务匹配后才读入整份 SKILL.md 正文;scripts/、references/、assets/ 仅在需要时再读。 本文件正文结构:Docker Compose — 多容器编排与 Compose 文件、When to Use、Complete File Structure、compose.yml、Services — Core Configuration、Networking。

文件分析

文件分析:这是一份仅含 SKILL.md 的指令型技能,代理激活后整份正文进入上下文。

官方 description(原文)

Expert guidance for Docker Compose — writing production-grade compose.yml files for multi-container applications. Covers complete Compose file syntax (services, networks, volumes, secrets, configs, profiles, extends, include), service configuration (image/build, ports, environment, env_file, depends_on with health checks, deploy resources, restart policies), networking in Compose (default networks, custom networks, internal networks), volume management, environment variable strategies (.env vs environment vs env_file), override files for multi-environment, profiles for optional services, and migration to Kubernetes with kompose. Use when the user asks about docker compose, docker-compose.yml, compose file, multi-container, service orchestration, compose configuration, or needs to write/modify a compose file. 使用场景:docker compose、docker-compose.yml、compose 文件、多容器、服务编排、compose 配置、depends_on、profiles.

Docker Compose — 多容器编排与 Compose 文件When to UseComplete File Structurecompose.ymlServices — Core ConfigurationNetworkingDefault Network (Auto-Created)Compose auto-creates a default bridge network.All services can reach each other by service name.Custom NetworksEnvironment VariablesApproaches

· 许可:Apache-2.0

来源分类:skills.sh agent-skill

SKILL.md 与 Agent 调用

官方规范 ↗
name
docker-compose
description
Expert guidance for Docker Compose — writing production-grade compose.yml files for multi-container applications. Covers complete Compose file syntax (services, networks, volumes, secrets, configs, profiles, extends, include), service configuration (image/build, ports, environment, env_file, depends_on with health checks, deploy resources, restart policies), networking in Compose (default networks, custom networks, internal networks), volume management, environment variable strategies (.env vs environment vs env_file), override files for multi-environment, profiles for optional services, and migration to Kubernetes with kompose. Use when the user asks about docker compose, docker-compose.yml, compose file, multi-container, service orchestration, compose configuration, or needs to write/modify a compose file. 使用场景:docker compose、docker-compose.yml、compose 文件、多容器、服务编排、compose 配置、depends_on、profiles.
许可
Apache-2.0
  1. 发现技能客户端向 Agent 提供名称与描述目录。
  2. 匹配与调用用户指定或任务匹配后,载入 SKILL.md 指令。
  3. 按需加载按步骤读取参考文档、使用脚本与素材。

具体调用语法与可用工具以目标 Agent 客户端为准。 查看调用机制说明 ↗

安装这个技能

Skills CLI ↗

先选择目标 Agent 和安装范围,保留技能包的附属文件,安装后检查客户端能否发现该技能。

交给 Agent 安装

复制安装指令给支持 Agent Skills 的代理,确认其中的目标目录与客户端匹配。

把 Agent Skill「docker-compose」安装到我的项目:SKILL.md 原文与官方 description 见 https://zicq.com/zh/skills/skl-69f8da6511019104-%E5%A4%9A%E5%85%8B%E7%BC%96%E6%9B%B2.html
请存为 .cursor/skills/docker-compose/SKILL.md 或 .claude/skills/docker-compose/SKILL.md,frontmatter 的 name 与 description 保持原样,不要改写。

GitHub 完整包 ↗

终端安装 · Skills CLI

需要 Node.js 与 npx。先查看仓库技能列表,确认实际名称。

npx skills add 'https://github.com/full-stack-skills/docker-skills' --list

npx skills add 'https://github.com/full-stack-skills/docker-skills' --skill 'docker-compose'

CLI 会交互选择目标 Agent,默认安装到项目;用户级安装使用 -g。先通过查看命令核对仓库内容,再用 npx skills list 检查已安装技能。

阅读排版

name: docker-compose description: Expert guidance for Docker Compose — writing production-grade compose.yml files for multi-container applications. Covers complete Compose file syntax (services, networks, volumes, secrets, configs, profiles, extends, include), service configuration (image/build, ports, environment, env_file, depends_on with health checks, deploy resources, restart policies), networking in Compose (default networks, custom networks, internal networks), volume management, environment variable strategies (.env vs environment vs env_file), override files for multi-environment, profiles for optional services, and migration to Kubernetes with kompose. Use when the user asks about docker compose, docker-compose.yml, compose file, multi-container, service orchestration, compose configuration, or needs to write/modify a compose file. 使用场景:docker compose、docker-compose.yml、compose 文件、多容器、服务编排、compose 配置、depends_on、profiles. license: Apache-2.0

Docker Compose — 多容器编排与 Compose 文件

Expert guidance for writing production-grade Docker Compose configurations.

When to Use

ALWAYS use this skill when the user mentions:

  • "docker compose", "docker-compose.yml", "compose 文件"
  • "compose 怎么写", "compose 配置"
  • "多容器", "multi-container", "服务编排"
  • "depends_on", "profiles", "override"
  • "compose network", "compose volume"

Complete File Structure

# compose.yml
name: myapp

services:       # Container definitions
networks:       # Network topology
volumes:        # Persistent storage
secrets:        # Sensitive data (Swarm)
configs:        # Non-sensitive configs (Swarm)

Services — Core Configuration

services:
  web:
    image: myapp:${TAG:-latest}     # Pull from registry
    # build: .                      # Or build from Dockerfile
    # build:                        # Build with options
    #   context: .
    #   dockerfile: Dockerfile.prod

    container_name: myapp-web       # Explicit name (optional)
    hostname: web

    ports:
      - "8080:8080"                 # host:container
      - "127.0.0.1:8443:443"       # bind to localhost
      - "8080:8080/udp"            # UDP

    environment:
      - NODE_ENV=production
      - DB_HOST=db                  # Service name = hostname!
    # env_file: .env.production    # Load from file

    volumes:
      - app-logs:/var/log/app       # Named volume
      - ./config:/etc/app:ro        # Bind mount (read-only)
      - /tmp/app:/tmp               # Bind mount (read-write)

    depends_on:
      db:
        condition: service_healthy  # Wait for health check
      redis:
        condition: service_started

    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:8080/health"]
      interval: 30s
      timeout: 3s
      retries: 3
      start_period: 10s

    restart: unless-stopped

    deploy:                         # Swarm-only
      replicas: 3
      resources:
        limits:
          cpus: '0.5'
          memory: 512M

    profiles:                       # Optional service
      - debug

Networking

Default Network (Auto-Created)

# Compose auto-creates a default bridge network.
# All services can reach each other by service name.
services:
  web:
    ports: ["8080:8080"]
  db:
    # No ports exposed externally — only accessible by 'web'

Custom Networks

networks:
  frontend:                        # Public-facing
  backend:
    internal: true                 # No external access

services:
  web:
    networks: [frontend, backend]  # In both networks
  db:
    networks: [backend]            # Backend only (isolated)
  cache:
    networks: [backend]

Environment Variables

Approaches

| Method | Where | Best For | |--------|-------|----------| | environment: | In compose.yml | Simple, few vars | | env_file: | External .env.prod | Many vars, env-specific | | .env file | Project root (auto-loaded) | Default values, $VARIABLE substitution |

.env File (Auto-Loaded)

# .env — auto-loaded by docker compose
TAG=v1.2.3
DB_PASSWORD=secret123
# compose.yml — uses ${TAG} and ${DB_PASSWORD}
services:
  web:
    image: myapp:${TAG:-latest}
  db:
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}

env_file

# .env.production
DB_HOST=prod-db.example.com
DB_PORT=5432
services:
  web:
    env_file: .env.production

Depends On — Service Order

services:
  db:
    image: postgres:16-alpine
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      retries: 5

  web:
    depends_on:
      db:
        condition: service_healthy   # Wait for DB to be ready
      redis:
        condition: service_started   # Wait for Redis to start

Profiles — Optional Services

services:
  app:
    image: myapp

  debug-tools:
    image: nicolaka/netshoot
    command: sleep infinity
    profiles: [debug]                # Only starts with --profile

  elasticsearch:
    image: elasticsearch:8.15.0
    profiles: [analytics]            # Only starts with --profile
docker compose --profile debug up    # Starts app + debug-tools
docker compose --profile analytics up # Starts app + elasticsearch

Multi-Environment with Override

compose.yml                # Base configuration
compose.override.yml       # Local dev overrides (auto-applied)
compose.prod.yml           # Production overrides
# Local: compose.yml + compose.override.yml (auto)
docker compose up -d

# Production: explicit files
docker compose -f compose.yml -f compose.prod.yml up -d

Migration to Kubernetes

kompose convert -f compose.yml   # Generates K8s YAML files
kubectl apply -f .

Workflow — 推荐编排流程

Step 1: 规划服务: 列出所有服务、端口、数据卷、环境变量 Step 2: 编写 compose.yml: 定义 services/networks/volumes Step 3: 配置依赖: depends_on + condition: service_healthy Step 4: 本地验证: docker compose up -d → docker compose ps → docker compose logs Step 5: 生产部署: 添加 resource limits、restart policy、日志轮转 → docker compose -f compose.yml -f compose.prod.yml up -d

Gotchas — Common Pitfalls

  • depends_on without healthcheck: depends_on only waits for container START, not READY. → Recovery: Always add condition: service_healthy + healthcheck: block; use docker compose ps to verify health status.
  • Port conflict: Multiple services can't bind the same host port. → Recovery: Use different host ports or remove ports: for internal-only services (they communicate via service name).
  • Bind mount paths: Relative paths are resolved from the compose file location. → Recovery: Use ./config not config/; verify with docker compose config to see resolved paths.
  • .env file security: .env files often contain secrets. → Recovery: Add .env to .gitignore; use Docker secrets for Swarm; use .env.example with placeholder values.
  • docker compose vs docker-compose: Modern syntax is docker compose (plugin). → Recovery: docker-compose (standalone binary) is deprecated; always use docker compose (with space).

Boundary — 能力边界(适用与不适用场景)

| 分类 | 场景 | 说明 | |------|------|------| | ✅ 能做 | 多容器应用编排 | services/networks/volumes/secrets/configs 完整定义 | | ✅ 能做 | 环境管理 | .env / env_file / override 文件策略 | | ✅ 能做 | 依赖控制 | depends_on + healthcheck + profiles | | ⚠️ 需条件 | 多主机部署 | 迁移到 Swarm(docker stack deploy)或 K8s | | ⚠️ 需条件 | 滚动更新/自动扩缩 | Compose 不支持,需 Swarm 或 K8s | | ❌ 超范围 | 编写 Dockerfile | 使用 docker-dockerfile | | ❌ 超范围 | K8s 部署 | 使用 kompose convert + K8s 技能 | | ❌ 超范围 | 云服务编排(Terraform) | IaaC 工具 |

When NOT to Use This Skill

| ❌ Skip | ✅ Use Instead | |---------|---------------| | Single-container apps | docker-run | | Writing Dockerfile | docker-dockerfile | | Kubernetes deployment | K8s manifests / Helm charts | | Docker basics | docker-basics |

Security & Stability

  • Use internal: true for backend networks to prevent external access.
  • Never commit .env files with secrets. Use CI secrets or Docker Swarm secrets.
  • Production Compose on single host: combine with systemd for auto-start on boot.
  • Multi-host production: migrate to Swarm (docker stack deploy) or Kubernetes.

📚 官方文档参考

| 文档 | 地址 | |------|------| | Docker Compose 概述 | https://docs.docker.com/compose/ | | Compose 文件参考 | https://docs.docker.com/reference/compose-file/ | | Compose CLI | https://docs.docker.com/reference/cli/docker/compose/ | | 环境变量 | https://docs.docker.com/compose/environment-variables/ | | Compose 网络 | https://docs.docker.com/compose/networking/ | | 生产环境 Compose | https://docs.docker.com/compose/production/ |

🧭 Docker Skills Journey

📍 You are here: docker-compose — 多容器编排

← Previous: docker-buildx / docker-networking → Next: docker-production / docker-cicd

相关技能

运维与云

Docker Essentials

用于容器管理,图像操作,调试的基本道克命令和工作流程.

运维与云

Find Skills

从开放的代理技能生态系统中发现并安装技能. 使用时:(1)用户问"我如何做X",X可能拥有现有技能,(2)用户说"为X找到技能"或"是否为X有技能",(3)用户问"你能否做X",X是专门能力,(4)用户想扩展代理能力,(5)用户想搜索工具,模板,或工作流程,(6)用户提到他们希望…

运维与云

Microsoft Foundry

构建,部署,评价,优化,微调,管理微软创始代理,模型,资源最终结束. 用法:铸造、azd ai代理、azd 提供/部署、托管代理脚手架/开发/运行/部署/拖放、即时代理创建、创建代理、更新代理、添加代理工具、援引代理、代理.yaml、评估代理、批量活化、连续活化、持续监测、代理C…

运维与云

Azure Deploy

执行已有.azure/部署计划.md和基础设施文件的ALREADY-PREPARED应用程序的Azure部署。 当用户向 CREATE 请求新应用程序时, 请不要使用此技能 。 这种技能运行了 azd上行, azd 部署, trarraform 应用, 和 az 部署命令带有内置…