跳到主内容
智客 ZICQ

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

Docker Compose Patterns

在创建,修改,或调试多克编译配置时使用此技能,即使用户只是说他们需要一起电线服务,在他们的堆栈中添加一个数据库,或者用多个容器建立本地开发环境. 包括服务定义、健康检查、依赖性订购、数量、网络、环境变量和发展重置.

3172 安装量

官方网址:skills.sh

技能介绍

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

做什么

在创建,修改,或调试多克编译配置时使用此技能,即使用户只是说他们需要一起电线服务,在他们的堆栈中添加一个数据库,或者用多个容器建立本地开发环境. 包括服务定义、健康检查、依赖性订购、数量、网络、环境变量和发展重置.

何时用

这个技能

代理如何加载

按 Agent Skills 渐进披露:启动时只加载 name 与 description(约 100 token);任务匹配后才读入整份 SKILL.md 正文;scripts/、references/、assets/ 仅在需要时再读。 本文件正文结构:Docker Compose Patterns、Overview、When to use this skill、Do not use this skill when、Core guidance、File naming。

文件分析

文件分析:除 SKILL.md 外,正文引用了 references/service-dependencies.md、references/volumes-and-networks.md、assets/compose-web-app.yaml、assets/compose-dev-override.yaml、assets/bad-vs-good.md、scripts/verify-compose.sh,属于带资源的技能包,这些文件按需再读。

官方 description(原文)

Use this skill when creating, modifying, or debugging Docker Compose configurations, even if the user just says they need to wire services together, add a database to their stack, or set up a local development environment with multiple containers. Covers service definitions, health checks, dependency ordering, volumes, networks, environment variables, and development overrides.

Docker Compose PatternsOverviewWhen to use this skillDo not use this skill whenCore guidanceFile namingService definitionsDependency modelingHealth checksVolumesNetworksEnvironment variables

兼容:Requires Docker Compose v2 (compose.yaml format). · 许可:Apache-2.0

来源分类:skills.sh agent-skill

SKILL.md 与 Agent 调用

官方规范 ↗
name
docker-compose-patterns
description
Use this skill when creating, modifying, or debugging Docker Compose configurations, even if the user just says they need to wire services together, add a database to their stack, or set up a local development environment with multiple containers. Covers service definitions, health checks, dependency ordering, volumes, networks, environment variables, and development overrides.
compatibility
Requires Docker Compose v2 (compose.yaml format).
许可
Apache-2.0
  1. 发现技能客户端向 Agent 提供名称与描述目录。
  2. 匹配与调用用户指定或任务匹配后,载入 SKILL.md 指令。
  3. 按需加载按步骤读取参考文档、使用脚本与素材。
指令中引用的文件 · 6
  • references/service-dependencies.md
  • references/volumes-and-networks.md
  • assets/compose-web-app.yaml
  • assets/compose-dev-override.yaml
  • assets/bad-vs-good.md
  • scripts/verify-compose.sh

以下路径提取自原文;文件是否齐全请以来源仓库中的完整目录为准。

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

安装这个技能

Skills CLI ↗

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

该技能引用了附属文件,请从来源获取完整目录;仅复制 SKILL.md 可能缺少依赖。

交给 Agent 安装

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

把 Agent Skill「docker-compose-patterns」安装到我的项目:SKILL.md 原文与官方 description 见 https://zicq.com/zh/skills/skl-d1c19f50777793d4-%E5%B5%8C%E5%85%A5%E5%9B%BE%E6%A1%88.html
请存为 .cursor/skills/docker-compose-patterns/SKILL.md 或 .claude/skills/docker-compose-patterns/SKILL.md,frontmatter 的 name 与 description 保持原样,不要改写。
该技能还带 scripts/、references/、assets/ 等文件,请从 https://github.com/docker/skills 取完整目录,不要只建一个 SKILL.md。

GitHub 完整包 ↗

终端安装 · Skills CLI

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

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

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

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

阅读排版
--- name: docker-compose-patterns description: Use this skill when creating, modifying, or debugging Docker Compose configurations, even if the user just says they need to wire services together, add a database to their stack, or set up a local development environment with multiple containers. Covers service definitions, health checks, dependency ordering, volumes, networks, environment variables, and development overrides. license: Apache-2.0 compatibility: Requires Docker Compose v2 (compose.yaml format). --- # Docker Compose Patterns ## Overview This skill provides rules for creating, reviewing, and debugging Docker Compose configurations. Use it when the main artifact is `compose.yaml` or `compose.override.yaml` and the task is about service wiring rather than image-build internals. ## When to use this skill Activate this skill when: - Creating a new `compose.yaml` for a project - Adding or modifying services in an existing Compose file - Setting up development overrides with `compose.override.yaml` - Debugging service startup ordering or connectivity issues ## Do not use this skill when Do not use this skill when: - The project has no Docker setup yet and the main need is an initial scaffold - The main task is writing or optimizing a `Dockerfile` - The main task is improving build caching, image size, or runtime user configuration ## Core guidance ### File naming Use `compose.yaml` as the canonical filename. Do not use `docker-compose.yml` or `docker-compose.yaml` — those are legacy names. ### Service definitions - Give services clear, lowercase names that reflect their role: `web`, `db`, `cache`, `worker`. - Always pin image tags to a specific version. Never use `latest` or omit the tag. - Set `restart: unless-stopped` for long-running infrastructure services and non-development deployments. - Add `container_name` only when external tools need a predictable name. Otherwise, let Compose generate names. ### Dependency modeling - Use `depends_on` with `condition: service_healthy` for services that must be ready before dependents start. - Every service listed in `depends_on` with a health condition must have a `healthcheck` defined. - Do not rely on `depends_on` without conditions — it only guarantees container start, not readiness. ### Health checks - Always add a `healthcheck` to database services (Postgres, MySQL, Redis, MongoDB). - Use the service's native client tool for health checks when available (e.g., `pg_isready`, `redis-cli ping`, `mysqladmin ping`). - Set reasonable `interval`, `timeout`, `retries`, and `start_period` values. Start with: `interval: 5s`, `timeout: 3s`, `retries: 3`, `start_period: 10s`. #### Health checks for distroless or scratch images Distroless, scratch-based, and hardened images contain no shell, curl, or wget. Do not bake tools into these images — that defeats their purpose. Instead, use a **healthcheck sidecar** that shares the application's network namespace: ```yaml services: api: build: context: . target: runtime # distroless / hardened image ports: - "8080:8080" # No healthcheck here — the image has no tools to run one api-health: image: curlimages/curl:8.22.0 network_mode: "service:api" # shares api's localhost entrypoint: ["sleep", "infinity"] # keep sidecar alive for healthcheck healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8080/health"] interval: 30s timeout: 5s retries: 3 start_period: 45s deploy: resources: limits: memory: 32M ``` Key points: - The sidecar must stay alive with `entrypoint: ["sleep", "infinity"]` so Compose can execute the healthcheck inside it. - `network_mode: "service:api"` makes `localhost` inside the sidecar resolve to the api container's loopback — no extra networking needed. - Keep the sidecar lightweight with a resource limit (32MB is sufficient for curl). - Services that depend on `api` being ready should reference the **sidecar**, not the api directly: ```yaml worker: depends_on: api-health: condition: service_healthy ``` ### Volumes - Use named volumes for data that must persist across container recreations (database data, uploaded files). - Use bind mounts only for development-time source code syncing. - Define all named volumes in the top-level `volumes:` key. - Do not mount the Docker socket unless the service genuinely requires it. ### Networks - For single-application stacks, the default network is sufficient. Do not create custom networks unless you need isolation between service groups. - When creating custom networks, prefer bridge driver and give networks descriptive names. - Use the top-level `networks:` key to define all custom networks. ### Environment variables - Use `environment:` for non-sensitive values that are few in number. - Use `env_file:` pointing to a `.env` file for longer lists of variables. - Never hardcode secrets (passwords, API keys) directly in `compose.yaml`. Use `env_file:` or Docker secrets. - When defaults are needed in the `environment:` block for local development, use variable substitution with fallbacks: `${DB_PASSWORD:-postgres}`. Never write bare plaintext values for password fields. - Add `.env` to `.gitignore`. ### Development overrides - Use `compose.override.yaml` for development-only settings. Compose loads it automatically alongside `compose.yaml`. - Put bind mounts for source code, debug ports, and development environment variables in the override file. - Use `develop.watch` for file-syncing and auto-rebuild in development when supported. - Keep production-oriented settings in the base `compose.yaml` and override only what changes for development. ### Compose Watch - Prefer `develop.watch` over manual bind mounts for development workflows. - Use `action: sync` for files that should be copied into the container on change (source code). - Use `action: rebuild` for files that require a full image rebuild (dependency files like `package.json`, `requirements.txt`). - Use `action: sync+restart` for configuration files that need a process restart. ### Destructive commands Some Compose commands delete data irreversibly. Before running any of the following, state exactly which data will be deleted and get explicit confirmation from the user — do not run them as a side effect of debugging, restarting, or "cleaning up" a stack: - `docker compose down -v` / `docker compose down --volumes` — deletes named volumes, including database data. - `docker volume rm` / `docker volume prune` run against a Compose project's volumes — deletes volumes directly. For the standalone case (no Compose project in play), see `docker-destructive-guardrails` instead. A volume referenced via `external: true` isn't managed by the Compose project either (`down -v` won't touch it) — treat it as the standalone case too: run `docker volume rm` without `-f` first, and get explicit confirmation before deleting it. - `docker compose rm -v` — deletes anonymous volumes attached to removed containers. If the goal is only to restart services or reclaim containers/networks, use `docker compose down` (no `-v`) or `docker compose restart` instead — these leave named volumes intact. ## Related skills - For first-time Docker project scaffolding and baseline file creation, use `docker-project-foundations`. - For Dockerfile internals, build caching, multi-stage builds, and `.dockerignore`, use `docker-build-strategies`. - For destructive Docker CLI commands outside Compose (`docker system prune`, `docker rm -f`, image/network/builder pruning, standalone volume deletion) and a cross-product index of destructive-command guardrails, use `docker-destructive-guardrails`. ## References - `references/service-dependencies.md` — Detailed guidance on `depends_on`, health check patterns for common databases, and startup ordering strategies. - `references/volumes-and-networks.md` — Patterns for volume mounts, named volumes, bind mounts, and network configuration. ## Assets - `assets/compose-web-app.yaml` — Complete multi-service web app (app + Postgres + Redis) with health checks, dependencies, and named volumes. - `assets/compose-dev-override.yaml` — Development override showing bind mounts, debug ports, and Compose Watch configuration. - `assets/bad-vs-good.md` — Before/after comparisons of common Compose mistakes and their fixes. ## Scripts - **`scripts/verify-compose.sh`** — Validates the Compose project in the current directory with `docker compose config --quiet`, without printing resolved configuration. Run it from the project root (the directory that contains `compose.yaml`), with the script path resolved under this skill's directory: ```bash bash "/scripts/verify-compose.sh" [--help] ``` Replace `` with the absolute path of the folder that contains this `SKILL.md`; the `scripts/` path is relative to that folder, not to the project. Do not change into the skill directory first: the script validates whatever Compose project is in the current directory. If the skill directory cannot be resolved, run `docker compose config --quiet` directly. Exit status is `0` when the Compose configuration is valid or help is requested, the non-zero status from `docker compose config --quiet` when validation fails, and `2` for invalid arguments. Plain `docker compose config` can expose interpolated and `env_file` credentials in tool output or logs; use quiet validation by default. Compose warnings and errors are still emitted and may contain sensitive details. ## Checks - `checks/verification.md` — Detailed verification runbook for manual review.

相关技能

运维与云

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 部署命令带有内置…