docker-compose by oimiragieo/agent-studio
npx skills add https://github.com/oimiragieo/agent-studio --skill docker-compose该技能调用 docker compose。最简单的方法是安装 Docker Desktop(包含 Docker Engine + Compose):
验证:docker compose version
命令: docker compose up -d / down; docker compose ps / ; ; ; — 验证配置。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
logs -f <service>docker compose exec <service> shdocker compose build --no-cachedocker compose -f compose.prod.yaml configYAML: 对数据库使用命名卷(postgres_data:/var/lib/postgresql/data)。使用健康检查(healthcheck: 包含 test、interval、timeout、retries)。默认一个网络;通过名称引用服务(例如 http://api:3000)。使用 env_file 或 environment;将密钥保存在 secrets: 中。
技巧: -f compose.yaml -f override.yaml 合并文件(后面的覆盖前面的)。使用 --project-name 进行隔离。开发时优先使用 build: context: . dockerfile: Dockerfile;生产环境固定镜像标签。在 up 之前运行 docker compose config 以捕获错误。
Docker 认证助理 (DCA): 编排 25%,镜像/注册表 20%,安装/配置 15%,网络 15%,安全 15%,存储 10%。免费资源:官方 DCA 学习指南, Coursera DCA 预备课程(可旁听)。技能数据: Compose YAML(服务、卷、网络、健康检查),CLI(up/down/ps/logs/exec)。
建议的钩子: 启动前:docker compose config(验证)。停止后:可选清理。当路由到 devops 或 devops-troubleshooter 时使用。
工作流: 与 devops(主要)、devops-troubleshooter(主要)一起使用。流程:验证 compose → 根据任务执行 up/down/exec。有关容器调试,请参阅 operations/incident-response。
此技能提供全面的 Docker Compose 管理,使 AI 代理能够编排多容器应用程序、管理服务、检查日志以及使用渐进式披露来排查容器化环境问题,以实现最佳上下文使用。
上下文节省 : 约减少 92%
docker compose 插件 — V1 docker-compose 已终止支持)compose.yaml(首选)或 compose.yml / docker-compose.yml# 列出运行中的服务
docker compose ps
# 查看服务日志
docker compose logs <service>
# 启动服务
docker compose up -d
# 停止服务
docker compose down
# 重新构建服务
docker compose build
# 在容器中执行命令
docker compose exec <service> <command>
# 实时开发重载 (Compose Watch)
docker compose watch
# 启动时激活配置文件
docker compose --profile debug up -d
# 验证合并后的配置
docker compose config
Docker Compose V2 优先使用 compose.yaml(以及 compose.yml)而非旧的 docker-compose.yml。顶级字段 version: 已弃用,在新文件中应完全省略。
# compose.yaml (首选 — 无需 version: 字段)
services:
web:
build: .
ports:
- '8080:80'
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: example
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Compose Watch(自 Compose 2.22+ 起正式发布)取代了开发期间手动重建-重启的循环。为每个服务配置一个 develop.watch 块。有三种可用操作:
| 操作 | 行为 |
|---|---|
sync | 立即将更改的文件复制到运行中的容器 |
rebuild | 触发 docker compose build + 重新创建容器 |
sync+restart | 同步文件然后重启容器进程(无需完全重建) |
services:
api:
build: .
ports:
- '3000:3000'
develop:
watch:
# 即时同步源代码 — 解释型代码无需重建
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
# 当依赖清单更改时重建
- action: rebuild
path: package.json
# 仅当配置更改时重启
- action: sync+restart
path: ./config
target: /app/config
启动方式:
# 监视模式(在前台保持输出)
docker compose watch
# 或与 up 结合使用
docker compose up --watch
每种操作的使用时机:
sync — 解释型语言(Node.js、Python、Ruby),运行时能获取更改sync+restart — 需要进程重启但无需完全重建的配置或模板文件rebuild — 依赖清单更改(package.json、requirements.txt、go.mod)配置文件允许单个 compose.yaml 服务于多个环境。没有配置文件的服务始终启动。带有配置文件的服务仅在激活该配置文件时启动。
services:
# 始终启动 — 无配置文件
api:
build: .
ports:
- '3000:3000'
depends_on:
db:
condition: service_healthy
db:
image: postgres:16-alpine
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 5s
timeout: 3s
retries: 5
volumes:
- db_data:/var/lib/postgresql/data
# 仅在使用 --profile debug 时启动
pgadmin:
image: dpage/pgadmin4:latest
profiles: ['debug']
ports:
- '5050:80'
environment:
PGADMIN_DEFAULT_EMAIL: admin@admin.com
PGADMIN_DEFAULT_PASSWORD: admin
# 仅在使用 --profile monitoring 时启动
prometheus:
image: prom/prometheus:latest
profiles: ['monitoring']
ports:
- '9090:9090'
grafana:
image: grafana/grafana:latest
profiles: ['monitoring']
ports:
- '3001:3000'
volumes:
db_data:
# 默认:仅 api + db
docker compose up -d
# 调试:api + db + pgadmin
docker compose --profile debug up -d
# 监控:api + db + prometheus + grafana
docker compose --profile monitoring up -d
# 多个配置文件
docker compose --profile debug --profile monitoring up -d
# 通过环境变量
COMPOSE_PROFILES=debug,monitoring docker compose up -d
配置文件命名规则: [a-zA-Z0-9][a-zA-Z0-9_.-]+ — 建议使用小写短横线命名法。
顶级键 include(在 Compose 2.20 中引入)允许您将大型 compose 文件拆分为模块化、团队拥有的部分。每个包含的文件都以其自己的项目目录上下文加载,正确解析相对路径。
# compose.yaml (根目录 — 应用层)
include:
- ./infra/compose.yaml # 数据库、Redis、消息代理
- ./monitoring/compose.yaml # Prometheus、Grafana
services:
api:
build: .
depends_on:
- db # 在 infra/compose.yaml 中定义
- redis # 在 infra/compose.yaml 中定义
# infra/compose.yaml (基础设施层 — 由平台团队拥有)
services:
db:
image: postgres:16-alpine
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 5s
timeout: 3s
retries: 5
volumes:
- db_data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
interval: 5s
timeout: 3s
retries: 5
volumes:
db_data:
include 是递归的 — 被包含的文件本身也可以包含其他文件。资源名称之间的冲突会导致错误(不会静默合并)。
始终在有状态服务上定义健康检查,以便 depends_on: condition: service_healthy 正常工作。没有健康检查,依赖服务可能会在其依赖项准备就绪之前启动。
services:
db:
image: postgres:16-alpine
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U ${POSTGRES_USER:-postgres}']
interval: 10s # 检查频率
timeout: 5s # 等待响应的时间
retries: 5 # 标记为不健康前的失败次数
start_period: 30s # 容器启动期间的宽限期
redis:
image: redis:7-alpine
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
interval: 10s
timeout: 3s
retries: 3
api:
build: .
depends_on:
db:
condition: service_healthy # 等待直到 db 通过健康检查
redis:
condition: service_healthy
健康检查指南:
CMD(数组形式)而不是 CMD-SHELL(字符串形式)— 避免 shell 注入风险CMD-SHELL(pg_isready、curl -f 等)start_period(JVM 应用、首次运行迁移)curl,除非明确安装;优先使用 wget -q --spider 或原生检查test: ["CMD-SHELL", "wget -q --spider http://localhost:3000/health || exit 1"]使用多阶段 Dockerfile 以保持生产镜像最小化和安全。在 compose.yaml 中引用特定的构建阶段用于开发。
# Dockerfile
# 阶段 1: deps — 安装依赖项
FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci
# 阶段 2: builder — 编译/转译
FROM deps AS builder
COPY . .
RUN npm run build
# 阶段 3: runner — 最小化生产镜像
FROM node:20-alpine AS runner
RUN addgroup -g 1001 -S appgroup && adduser -S -u 1001 -G appgroup appuser
WORKDIR /app
COPY --from=builder --chown=appuser:appgroup /app/dist ./dist
COPY --from=deps --chown=appuser:appgroup /app/node_modules ./node_modules
USER appuser
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD wget -q --spider http://localhost:3000/health || exit 1
CMD ["node", "dist/index.js"]
# compose.yaml — 开发时定位到 builder 阶段以加快迭代
services:
api:
build:
context: .
dockerfile: Dockerfile
target: builder # 在开发时停止在 builder 阶段(包含 devDeps)
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.json
# compose.prod.yaml — 生产使用完整的 runner 阶段
services:
api:
build:
context: .
dockerfile: Dockerfile
target: runner # 最小化、非 root 的生产镜像
restart: unless-stopped
始终定义资源限制以防止容器资源耗尽:
services:
api:
image: myapp:latest
deploy:
resources:
limits:
cpus: '1.0'
memory: 512M
reservations:
cpus: '0.25'
memory: 128M
restart: unless-stopped
该技能提供 15 个工具,涵盖服务管理、监控、构建操作和故障排除类别:
启动 compose.yaml 中定义的服务。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
detached | boolean | 在分离模式下运行 | true |
build | boolean | 启动前构建镜像 | false |
force_recreate | boolean | 重新创建容器 | false |
project_name | string | 项目名称覆盖 | 目录名 |
services | array | 要启动的特定服务 | 所有服务 |
profiles | array | 要激活的配置文件 | 无 |
watch | boolean | 启用 Compose Watch 模式 | false |
示例 :
docker compose up -d
docker compose up --build
docker compose up web api
docker compose --profile debug up -d
docker compose up --watch
安全性 : 生产环境需要确认。
停止并移除容器、网络、卷。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
volumes | boolean | 移除卷(已阻止) | false |
remove_orphans | boolean | 移除孤立的容器 | false |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose down
docker compose down --remove-orphans
安全性 : 卷移除(-v 标志)默认被阻止。需要确认。
启动现有容器而不重新创建它们。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
services | array | 要启动的特定服务 | 所有服务 |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose start
docker compose start web
停止运行中的容器而不移除它们。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
timeout | number | 关闭超时(秒) | 10 |
services | array | 要停止的特定服务 | 所有服务 |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose stop
docker compose stop --timeout 30 web
重启服务(停止 + 启动)。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
timeout | number | 关闭超时(秒) | 10 |
services | array | 要重启的特定服务 | 所有服务 |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose restart
docker compose restart api
列出容器及其状态信息。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
all | boolean | 显示所有容器(包括已停止的) | false |
services | array | 按服务过滤 | 所有服务 |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose ps
docker compose ps --all
输出字段 : 名称、镜像、状态、端口
查看服务日志,支持流式传输。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
services | array | 要查看日志的服务 | 所有服务 |
follow | boolean | 跟随日志输出(流式) | false |
tail | number | 要显示的行数 | 100 |
timestamps | boolean | 显示时间戳 | false |
since | string | 显示自时间戳/持续时间以来的日志 | 无 |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose logs web
docker compose logs --tail 50 --follow api
docker compose logs --since "2026-01-01T10:00:00"
注意 : 跟随模式在 60 秒后自动终止,以防止无限流式传输。
显示容器中运行的进程。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
services | array | 要检查的服务 | 所有服务 |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose top
docker compose top web
输出 : 进程列表,包含 PID、用户、时间、命令
构建或重新构建服务镜像。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
no_cache | boolean | 不使用缓存构建 | false |
pull | boolean | 拉取更新的镜像版本 | false |
parallel | boolean | 并行构建 | true |
services | array | 要构建的服务 | 所有服务 |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose build
docker compose build --no-cache web
docker compose build --pull
安全性 : 无缓存构建(资源密集型)需要确认。
从注册表拉取服务镜像。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
ignore_pull_failures | boolean | 如果拉取失败则继续 | false |
services | array | 要拉取的服务 | 所有服务 |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose pull
docker compose pull web api
安全性 : 生产环境需要确认。
列出服务使用的镜像。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose images
输出字段 : 容器、仓库、标签、镜像 ID、大小
在运行中的容器中执行命令。
| 参数 | 类型 | 描述 | 必需 |
|---|---|---|---|
service | string | 服务名称 | 是 |
command | array | 要执行的命令 | 是 |
user | string | 执行命令的用户 | 容器默认值 |
workdir | string | 工作目录 | 容器默认值 |
env | object | 环境变量 | 无 |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose exec web bash
docker compose exec -u root api ls -la /app
docker compose exec db psql -U postgres
安全性 :
rm -rf、dd、mkfs)被阻止在新容器中运行一次性命令。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
service | string | 要运行的服务 | 必需 |
command | array | 要执行的命令 | 服务默认值 |
rm | boolean | 运行后移除容器 | true |
no_deps | boolean | 不启动链接的服务 | false |
user | string | 执行命令的用户 | 容器默认值 |
env | object | 环境变量 | 无 |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose run --rm web npm test
docker compose run --no-deps api python manage.py migrate
安全性 : 修改数据的命令需要确认。
验证并查看 Compose 文件配置。
| 参数 | 类型 | 描述 | 默认值 |
|---|---|---|---|
resolve_image_digests | boolean | 将镜像标签固定为摘要 | false |
no_interpolate | boolean | 不插值环境变量 | false |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose config
docker compose config --resolve-image-digests
输出 : 解析和合并后的 Compose 配置
打印服务端口的公共端口绑定。
| 参数 | 类型 | 描述 | 必需 |
|---|---|---|---|
service | string | 服务名称 | 是 |
private_port | number | 容器端口 | 是 |
protocol | string | 协议 (tcp/udp) | tcp |
project_name | string | 项目名称覆盖 | 目录名 |
示例 :
docker compose port web 80
docker compose port db 5432
输出 : <主机>:<端口> 绑定
# 1. 验证配置
docker compose config
# 2. 拉取最新镜像
docker compose pull
# 3. 构建自定义镜像
docker compose build
# 4. 在分离模式下启动服务
docker compose up -d
# 5. 检查服务状态
docker compose ps
# 6. 查看日志
docker compose logs --tail 100
# 1. 确保 compose.yaml 中配置了 develop.watch 块
# 2. 以监视模式启动(前台,显示同步事件)
docker compose watch
# 3. 或先分离启动再监视
docker compose up -d
docker compose watch --no-up
# 1. 检查容器状态
docker compose ps --all
# 2. 查看服务日志
docker compose logs --tail 200 failing-service
# 3. 检查运行中的进程
docker compose top failing-service
# 4. 检查配置
docker compose config
# 5. 重启服务
docker compose restart failing-service
# 6. 如果需要,重新创建容器
docker compose up -d --force-recreate failing-service
# 1. 拉取最新镜像
docker compose pull
# 2. 停止服务
docker compose down
# 3. 如果使用自定义 Dockerfile 则重新构建
docker compose build --pull
# 4. 使用新镜像启动
docker compose up -d
# 5. 验证服务
docker compose ps
# 1. 检查运行中的服务
docker compose ps
# 2. 检查端口映射
docker compose port web 80
docker compose port api 3000
# 3. 进入容器
docker compose exec web sh
# 4. 测试连接性(从容器内部)
docker compose exec web curl api:3000/health
# 5. 检查错误日志
docker compose logs web api
# 1. 停止所有服务
docker compose down
# 2. 移除孤立的容器
docker compose down --remove-orphans
# 3. 查看镜像
docker compose images
# 4. 清理(手动 - 卷移除被阻止)
# 卷需要手动清理并明确确认
# 开发:仅默认服务
docker compose up -d
# 开发 + 调试工具
docker compose --profile debug up -d
# 启动监控栈
docker compose --profile monitoring up -d
# 通过环境变量(在 CI 中很有用)
COMPOSE_PROFILES=monitoring docker compose up -d
# 停止并清理特定配置文件
docker compose --profile debug down
| 变量 | 描述 | 默认值 |
|---|---|---|
COMPOSE_PROJECT_NAME | 默认项目名称 | 目录名 |
COMPOSE_FILE | Compose 文件路径 | compose.yaml |
COMPOSE_PROFILES | 逗号分隔的活动配置文件 | (无) |
COMPOSE_PATH_SEPARATOR | 多个文件的路径分隔符 | : (Linux/Mac), ; (Windows) |
DOCKER_HOST | Docker 守护进程套接字 | unix:///var/run/docker.sock |
COMPOSE_HTTP_TIMEOUT | API 调用的 HTTP 超时 | 60 |
COMPOSE_PARALLEL_LIMIT | 最大并行操作数 | 无限制 |
安装 Docker Engine :
brew install --cask docker
# Linux (Ubuntu/Debian)
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# Windows
# 从 docker.com 下载 Docker Desktop
2. 验证 Docker Compose :
# 检查 Docker 版本
docker --version
# 检查 Compose 版本(必须是 V2,例如 2.24+)
docker compose version
3. 创建 compose.yaml(无 version: 字段 — V2 不需要它):
services:
web:
build: .
ports:
- '8080:80'
depends_on:
db:
condition: service_healthy
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: example
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 10s
timeout: 5s
retries: 5
4. 测试技能 :
docker compose config
docker compose ps
以下操作默认被阻止,以防止意外数据丢失:
docker compose down -v (被阻止 - 需要手动确认)docker compose down -v --rmi all (被阻止 - 极具破坏性)rm -rf、dd、mkfs、sudo rm (被阻止)docker compose rm -f (被阻止 - 使用 stop 然后 rm)这些操作需要明确确认:
--no-cache 构建(资源密集型)--force-recreate 启动服务以下操作会自动终止以防止资源问题:
--follow):60 秒超时exec):30 秒超时run):60 秒超时常见错误 :
| 错误 | 原因 | 修复方法 |
|---|---|---|
docker: command not found | Docker 未安装 | 安装 Docker Engine |
Cannot connect to Docker daemon | Docker 未运行 | 启动 Docker 服务 |
network ... not found | 网络清理问题 | 运行 docker compose down 然后 up |
port is already allocated | 端口冲突 | 更改端口映射或停止冲突的服务 |
no configuration file provided | 缺少 compose 文件 | 创建 compose.yaml |
service ... must be built | 镜像未构建 | 运行 docker compose build |
service unhealthy | 健康检查失败 | 检查 docker compose logs <service> |
include path not found | 缺少包含的文件 | 验证 include: 块中的路径 |
恢复方法 :
docker compose configdocker infodocker compose logsdocker compose up -d --force-recreatedocker compose down && docker compose up -d此技能与以下代理集成:
该技能使用渐进式披露以最小化上下文使用:
上下文优化 :
--tail 限制日志输出ps 而非 ps --all--since 进行时间限制的日志查询未找到 Docker Compose :
# 检查 Docker Compose 版本
docker compose version
# V1 (docker-compose) 已终止支持 — 升级到 V2
# Docker Compose V2 作为插件集成到 Docker CLI 中
权限被拒绝 :
# 将用户添加到 docker 组 (Linux)
sudo usermod -aG docker $USER
newgrp docker
# 验证权限
docker ps
Compose 文件问题 :
# 验证语法
docker compose config
# 检查错误(静默模式 — 仅退出代码)
docker compose config -q
# 查看解析后的配置
docker compose config --resolve-image-digests
网络问题 :
# 列出网络
docker network ls
# 移除未使用的网络
docker network prune
# 重新创建服务
docker compose down
docker compose up -d
健康检查失败 :
# 检查健康检查状态
docker inspect <container_id> | grep -A 10 Health
# 查看健康检查输出
docker compose logs <service>
# 手动运行健康检查命令
docker compose exec <service> pg_isready -U postgres
Compose Watch 不同步 :
# 验证 compose.yaml 中是否存在 develop.watch 块
docker compose config | grep -A 20 develop
# 确保 Compose 版本为 2.22+
docker compose version
# Watch 需要 build: 属性(不仅仅是 image:)
--no-cacheCOMPOSE_PARALLEL_LIMIT 进行控制develop.watch;绑定挂载在 macOS/Windows 上有 I/O 性能问题.claude/skills/kubernetes-flux/ (Kubernetes 编排)cloud-devops-expert](The skill invokes docker compose. Easiest: install Docker Desktop (includes Docker Engine + Compose):
Verify: docker compose version
Commands: docker compose up -d / down; docker compose ps / logs -f <service>; docker compose exec <service> sh; docker compose build --no-cache; docker compose -f compose.prod.yaml config — validate.
YAML: Use named volumes for DBs (postgres_data:/var/lib/postgresql/data). Use healthchecks (healthcheck: with test, interval, timeout, retries). One network default; reference services by name (e.g. http://api:3000). Use env_file or environment; keep secrets in secrets:.
Hacks: -f compose.yaml -f override.yaml merges files (later overrides). Use --project-name for isolation. Prefer build: context: . dockerfile: Dockerfile for dev; pin image tags in prod. Run docker compose config before up to catch errors.
Docker Certified Associate (DCA): Orchestration 25%, Image/Registry 20%, Install/Config 15%, Networking 15%, Security 15%, Storage 10%. Free: Official DCA Study Guide, Coursera DCA Prep (audit). Skill data: Compose YAML (services, volumes, networks, healthchecks), CLI (up/down/ps/logs/exec).
Suggested hooks: Pre-up: docker compose config (validate). Post-down: optional cleanup. Use when devops or devops-troubleshooter is routed.
Workflows: Use with devops (primary), devops-troubleshooter (primary). Flow: validate compose → up/down/exec per task. See operations/incident-response for container debugging.
This skill provides comprehensive Docker Compose management, enabling AI agents to orchestrate multi-container applications, manage services, inspect logs, and troubleshoot containerized environments with progressive disclosure for optimal context usage.
Context Savings : ~92% reduction
docker compose plugin — V1 docker-compose is end-of-life)compose.yaml (preferred) or compose.yml / docker-compose.yml in project# List running services
docker compose ps
# View service logs
docker compose logs <service>
# Start services
docker compose up -d
# Stop services
docker compose down
# Rebuild services
docker compose build
# Execute command in container
docker compose exec <service> <command>
# Live development reloading (Compose Watch)
docker compose watch
# Start with a profile active
docker compose --profile debug up -d
# Validate merged config
docker compose config
Docker Compose V2 prefers compose.yaml (and compose.yml) over the legacy docker-compose.yml. The version: top-level field is deprecated and should be omitted entirely in new files.
# compose.yaml (preferred — no version: field needed)
services:
web:
build: .
ports:
- '8080:80'
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: example
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Compose Watch (GA as of Compose 2.22+) replaces the manual rebuild-restart cycle during development. Configure a develop.watch block per service. Three actions are available:
| Action | Behavior |
|---|---|
sync | Instantly copies changed files into the running container |
rebuild | Triggers docker compose build + recreates the container |
sync+restart | Syncs files then restarts the container process (no full rebuild) |
services:
api:
build: .
ports:
- '3000:3000'
develop:
watch:
# Sync source instantly — no rebuild needed for interpreted code
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
# Rebuild when dependency manifest changes
- action: rebuild
path: package.json
# Restart only when config changes
- action: sync+restart
path: ./config
target: /app/config
Start with:
# Watch mode (keeps output in foreground)
docker compose watch
# Or combined with up
docker compose up --watch
When to use each action:
sync — interpreted languages (Node.js, Python, Ruby) where the runtime picks up changessync+restart — config or template files that require a process restart but not a full rebuildrebuild — dependency manifest changes (package.json, requirements.txt, go.mod)Profiles allow a single compose.yaml to serve multiple environments. Services without a profile always start. Services with profiles only start when that profile is activated.
services:
# Always starts — no profile
api:
build: .
ports:
- '3000:3000'
depends_on:
db:
condition: service_healthy
db:
image: postgres:16-alpine
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 5s
timeout: 3s
retries: 5
volumes:
- db_data:/var/lib/postgresql/data
# Only with --profile debug
pgadmin:
image: dpage/pgadmin4:latest
profiles: ['debug']
ports:
- '5050:80'
environment:
PGADMIN_DEFAULT_EMAIL: admin@admin.com
PGADMIN_DEFAULT_PASSWORD: admin
# Only with --profile monitoring
prometheus:
image: prom/prometheus:latest
profiles: ['monitoring']
ports:
- '9090:9090'
grafana:
image: grafana/grafana:latest
profiles: ['monitoring']
ports:
- '3001:3000'
volumes:
db_data:
# Default: api + db only
docker compose up -d
# Debug: api + db + pgadmin
docker compose --profile debug up -d
# Monitoring: api + db + prometheus + grafana
docker compose --profile monitoring up -d
# Multiple profiles
docker compose --profile debug --profile monitoring up -d
# Via environment variable
COMPOSE_PROFILES=debug,monitoring docker compose up -d
Profile naming rules: [a-zA-Z0-9][a-zA-Z0-9_.-]+ — lowercase kebab-case recommended.
The include top-level key (introduced in Compose 2.20) allows you to split large compose files into modular, team-owned pieces. Each included file is loaded with its own project directory context, resolving relative paths correctly.
# compose.yaml (root — application layer)
include:
- ./infra/compose.yaml # DB, Redis, message broker
- ./monitoring/compose.yaml # Prometheus, Grafana
services:
api:
build: .
depends_on:
- db # defined in infra/compose.yaml
- redis # defined in infra/compose.yaml
# infra/compose.yaml (infrastructure layer — owned by platform team)
services:
db:
image: postgres:16-alpine
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 5s
timeout: 3s
retries: 5
volumes:
- db_data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
interval: 5s
timeout: 3s
retries: 5
volumes:
db_data:
include is recursive — included files can themselves include other files. Conflicts between resource names cause an error (no silent merging).
Always define healthchecks on stateful services so that depends_on: condition: service_healthy works correctly. Without healthchecks, dependent services may start before their dependency is ready.
services:
db:
image: postgres:16-alpine
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U ${POSTGRES_USER:-postgres}']
interval: 10s # How often to check
timeout: 5s # Time to wait for response
retries: 5 # Failures before marking unhealthy
start_period: 30s # Grace period during container startup
redis:
image: redis:7-alpine
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
interval: 10s
timeout: 3s
retries: 3
api:
build: .
depends_on:
db:
condition: service_healthy # waits until db passes healthcheck
redis:
condition: service_healthy
Healthcheck guidelines:
CMD (array form) not CMD-SHELL (string form) where possible — avoids shell injection riskCMD-SHELL only when you need shell features (pg_isready, curl -f, etc.)start_period for services with slow startup (JVM apps, first-run migrations)curl in Alpine-based images unless explicitly installed; prefer wget -q --spider or native checkstest: ["CMD-SHELL", "wget -q --spider http://localhost:3000/health || exit 1"]Use multi-stage Dockerfiles to keep production images minimal and secure. Reference the specific build stage in compose.yaml for development.
# Dockerfile
# Stage 1: deps — install dependencies
FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci
# Stage 2: builder — compile/transpile
FROM deps AS builder
COPY . .
RUN npm run build
# Stage 3: runner — minimal production image
FROM node:20-alpine AS runner
RUN addgroup -g 1001 -S appgroup && adduser -S -u 1001 -G appgroup appuser
WORKDIR /app
COPY --from=builder --chown=appuser:appgroup /app/dist ./dist
COPY --from=deps --chown=appuser:appgroup /app/node_modules ./node_modules
USER appuser
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD wget -q --spider http://localhost:3000/health || exit 1
CMD ["node", "dist/index.js"]
# compose.yaml — dev targets the builder stage for faster iteration
services:
api:
build:
context: .
dockerfile: Dockerfile
target: builder # Stop at builder stage in dev (includes devDeps)
develop:
watch:
- action: sync
path: ./src
target: /app/src
ignore:
- node_modules/
- action: rebuild
path: package.json
# compose.prod.yaml — production uses the full runner stage
services:
api:
build:
context: .
dockerfile: Dockerfile
target: runner # Minimal, non-root production image
restart: unless-stopped
Always define resource limits to prevent container resource exhaustion:
services:
api:
image: myapp:latest
deploy:
resources:
limits:
cpus: '1.0'
memory: 512M
reservations:
cpus: '0.25'
memory: 128M
restart: unless-stopped
The skill provides 15 tools across service management, monitoring, build operations, and troubleshooting categories:
Start services defined in compose.yaml.
| Parameter | Type | Description | Default |
|---|---|---|---|
detached | boolean | Run in detached mode | true |
build | boolean | Build images before starting | false |
force_recreate | boolean | Recreate containers | false |
project_name | string | Project name override | directory name |
Example :
docker compose up -d
docker compose up --build
docker compose up web api
docker compose --profile debug up -d
docker compose up --watch
Safety : Requires confirmation for production environments.
Stop and remove containers, networks, volumes.
| Parameter | Type | Description | Default |
|---|---|---|---|
volumes | boolean | Remove volumes (BLOCKED) | false |
remove_orphans | boolean | Remove orphaned containers | false |
project_name | string | Project name override | directory name |
Example :
docker compose down
docker compose down --remove-orphans
Safety : Volume removal (-v flag) is BLOCKED by default. Requires confirmation.
Start existing containers without recreating them.
| Parameter | Type | Description | Default |
|---|---|---|---|
services | array | Specific services to start | all services |
project_name | string | Project name override | directory name |
Example :
docker compose start
docker compose start web
Stop running containers without removing them.
| Parameter | Type | Description | Default |
|---|---|---|---|
timeout | number | Shutdown timeout (seconds) | 10 |
services | array | Specific services to stop | all services |
project_name | string | Project name override | directory name |
Example :
docker compose stop
docker compose stop --timeout 30 web
Restart services (stop + start).
| Parameter | Type | Description | Default |
|---|---|---|---|
timeout | number | Shutdown timeout (seconds) | 10 |
services | array | Specific services to restart | all services |
project_name | string | Project name override | directory name |
Example :
docker compose restart
docker compose restart api
List containers with status information.
| Parameter | Type | Description | Default |
|---|---|---|---|
all | boolean | Show all containers (including stopped) | false |
services | array | Filter by services | all services |
project_name | string | Project name override | directory name |
Example :
docker compose ps
docker compose ps --all
Output Fields : NAME, IMAGE, STATUS, PORTS
View service logs with streaming support.
| Parameter | Type | Description | Default |
|---|---|---|---|
services | array | Services to view logs for | all services |
follow | boolean | Follow log output (stream) | false |
tail | number | Number of lines to show | 100 |
timestamps | boolean | Show timestamps | false |
Example :
docker compose logs web
docker compose logs --tail 50 --follow api
docker compose logs --since "2026-01-01T10:00:00"
Note : Follow mode automatically terminates after 60 seconds to prevent indefinite streaming.
Display running processes in containers.
| Parameter | Type | Description | Default |
|---|---|---|---|
services | array | Services to inspect | all services |
project_name | string | Project name override | directory name |
Example :
docker compose top
docker compose top web
Output : Process list with PID, USER, TIME, COMMAND
Build or rebuild service images.
| Parameter | Type | Description | Default |
|---|---|---|---|
no_cache | boolean | Build without cache | false |
pull | boolean | Pull newer image versions | false |
parallel | boolean | Build in parallel | true |
services | array | Services to build | all services |
Example :
docker compose build
docker compose build --no-cache web
docker compose build --pull
Safety : Requires confirmation for no-cache builds (resource-intensive).
Pull service images from registry.
| Parameter | Type | Description | Default |
|---|---|---|---|
ignore_pull_failures | boolean | Continue if pull fails | false |
services | array | Services to pull | all services |
project_name | string | Project name override | directory name |
Example :
docker compose pull
docker compose pull web api
Safety : Requires confirmation for production environments.
List images used by services.
| Parameter | Type | Description | Default |
|---|---|---|---|
project_name | string | Project name override | directory name |
Example :
docker compose images
Output Fields : CONTAINER, REPOSITORY, TAG, IMAGE ID, SIZE
Execute a command in a running container.
| Parameter | Type | Description | Required |
|---|---|---|---|
service | string | Service name | Yes |
command | array | Command to execute | Yes |
user | string | User to execute as | container default |
workdir | string | Working directory | container default |
Example :
docker compose exec web bash
docker compose exec -u root api ls -la /app
docker compose exec db psql -U postgres
Safety :
rm -rf, dd, mkfs) are BLOCKEDRun a one-off command in a new container.
| Parameter | Type | Description | Default |
|---|---|---|---|
service | string | Service to run | Required |
command | array | Command to execute | service default |
rm | boolean | Remove container after run | true |
no_deps | boolean | Don't start linked services | false |
Example :
docker compose run --rm web npm test
docker compose run --no-deps api python manage.py migrate
Safety : Requires confirmation for commands that modify data.
Validate and view the Compose file configuration.
| Parameter | Type | Description | Default |
|---|---|---|---|
resolve_image_digests | boolean | Pin image tags to digests | false |
no_interpolate | boolean | Don't interpolate env vars | false |
project_name | string | Project name override | directory name |
Example :
docker compose config
docker compose config --resolve-image-digests
Output : Parsed and merged Compose configuration
Print the public port binding for a service port.
| Parameter | Type | Description | Required |
|---|---|---|---|
service | string | Service name | Yes |
private_port | number | Container port | Yes |
protocol | string | Protocol (tcp/udp) | tcp |
project_name | string | Project name override | directory name |
Example :
docker compose port web 80
docker compose port db 5432
Output : <host>:<port> binding
# 1. Validate configuration
docker compose config
# 2. Pull latest images
docker compose pull
# 3. Build custom images
docker compose build
# 4. Start services in detached mode
docker compose up -d
# 5. Check service status
docker compose ps
# 6. View logs
docker compose logs --tail 100
# 1. Ensure develop.watch blocks are configured in compose.yaml
# 2. Start with watch mode (foreground, shows sync events)
docker compose watch
# 3. Or start detached then watch
docker compose up -d
docker compose watch --no-up
# 1. Check container status
docker compose ps --all
# 2. View service logs
docker compose logs --tail 200 failing-service
# 3. Inspect running processes
docker compose top failing-service
# 4. Check configuration
docker compose config
# 5. Restart the service
docker compose restart failing-service
# 6. If needed, recreate container
docker compose up -d --force-recreate failing-service
# 1. Pull latest images
docker compose pull
# 2. Stop services
docker compose down
# 3. Rebuild if using custom Dockerfiles
docker compose build --pull
# 4. Start with new images
docker compose up -d
# 5. Verify services
docker compose ps
# 1. Check running services
docker compose ps
# 2. Inspect port mappings
docker compose port web 80
docker compose port api 3000
# 3. Exec into container
docker compose exec web sh
# 4. Test connectivity (from inside container)
docker compose exec web curl api:3000/health
# 5. Check logs for errors
docker compose logs web api
# 1. Stop all services
docker compose down
# 2. Remove orphaned containers
docker compose down --remove-orphans
# 3. View images
docker compose images
# 4. Clean up (manual - volume removal BLOCKED)
# Volumes require manual cleanup with explicit confirmation
# Development: default services only
docker compose up -d
# Development + debug tools
docker compose --profile debug up -d
# Start monitoring stack
docker compose --profile monitoring up -d
# Via env var (useful in CI)
COMPOSE_PROFILES=monitoring docker compose up -d
# Stop and clean a specific profile
docker compose --profile debug down
| Variable | Description | Default |
|---|---|---|
COMPOSE_PROJECT_NAME | Default project name | directory name |
COMPOSE_FILE | Compose file path | compose.yaml |
COMPOSE_PROFILES | Comma-separated active profiles | (none) |
COMPOSE_PATH_SEPARATOR | Path separator for multiple files | : (Linux/Mac), (Windows) |
Install Docker Engine :
brew install --cask docker
# Linux (Ubuntu/Debian)
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# Windows
# Download Docker Desktop from docker.com
2. Verify Docker Compose :
# Check Docker version
docker --version
# Check Compose version (must be V2, e.g. 2.24+)
docker compose version
3. Create compose.yaml (no version: field — V2 does not require it):
services:
web:
build: .
ports:
- '8080:80'
depends_on:
db:
condition: service_healthy
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: example
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 10s
timeout: 5s
retries: 5
4. Test the skill :
docker compose config
docker compose ps
The following operations are BLOCKED by default to prevent accidental data loss:
docker compose down -v (BLOCKED - requires manual confirmation)docker compose down -v --rmi all (BLOCKED - extremely destructive)rm -rf, dd, mkfs, sudo rm inside containers (BLOCKED)docker compose rm -f (BLOCKED - use stop then rm)These operations require explicit confirmation:
--no-cache (resource-intensive)--force-recreateThe following operations auto-terminate to prevent resource issues:
--follow): 60-second timeoutexec): 30-second timeoutrun): 60-second timeoutCommon Errors :
| Error | Cause | Fix |
|---|---|---|
docker: command not found | Docker not installed | Install Docker Engine |
Cannot connect to Docker daemon | Docker not running | Start Docker service |
network ... not found | Network cleanup issue | Run docker compose down then up |
port is already allocated |
Recovery :
docker compose configdocker infodocker compose logsdocker compose up -d --force-recreatedocker compose down && docker compose up -dThis skill integrates with the following agents:
The skill uses progressive disclosure to minimize context usage:
Context Optimization :
--tail to limit log outputps over ps --all for active services only--since for time-bounded log queriesDocker Compose not found :
# Check Docker Compose version
docker compose version
# V1 (docker-compose) is end-of-life — upgrade to V2
# Docker Compose V2 is integrated into Docker CLI as a plugin
Permission denied :
# Add user to docker group (Linux)
sudo usermod -aG docker $USER
newgrp docker
# Verify permissions
docker ps
Compose file issues :
# Validate syntax
docker compose config
# Check for errors (quiet mode — exit code only)
docker compose config -q
# View resolved configuration
docker compose config --resolve-image-digests
Network issues :
# List networks
docker network ls
# Remove unused networks
docker network prune
# Recreate services
docker compose down
docker compose up -d
Healthcheck failures :
# Inspect healthcheck status
docker inspect <container_id> | grep -A 10 Health
# View healthcheck output
docker compose logs <service>
# Manually run the healthcheck command
docker compose exec <service> pg_isready -U postgres
Compose Watch not syncing :
# Verify develop.watch block is present in compose.yaml
docker compose config | grep -A 20 develop
# Ensure Compose version is 2.22+
docker compose version
# Watch requires build: attribute (not image: only)
--no-cache unless necessaryCOMPOSE_PARALLEL_LIMIT to controldevelop.watch for cross-platform development; bind mounts have I/O performance issues on macOS/Windows.claude/skills/kubernetes-flux/ (Kubernetes orchestration)cloud-devops-expert - Cloud platforms (AWS, GCP, Azure) and Terraform infrastructuredocker compose (V2 plugin) — never use docker-compose (V1 standalone), which is deprecated and will be removed..env files — use external secret management or .env.example templates for documentation only.| Anti-Pattern | Why It Fails | Correct Approach |
|---|---|---|
Using docker-compose V1 command | V1 is deprecated; missing V2 features (profiles, merge, watch) and will be removed | Use docker compose (space, not hyphen); verify docker compose version |
| Hardcoded secrets in compose file | Secrets committed to git are permanently exposed in history | Use environment variable references (${SECRET}) loaded from untracked .env |
| No health checks on database/cache services | App containers start before DB is ready; causes startup race conditions and crashes | Add healthcheck: with appropriate test commands; use |
Before starting: Read .claude/context/memory/learnings.md
After completing:
.claude/context/memory/learnings.md.claude/context/memory/issues.md.claude/context/memory/decisions.mdASSUME INTERRUPTION: If it's not in memory, it didn't happen.
Weekly Installs
78
Repository
GitHub Stars
19
First Seen
Jan 27, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykFail
Installed on
github-copilot77
gemini-cli76
cursor76
amp75
codex75
opencode75
Azure 升级评估与自动化工具 - 轻松迁移 Functions 计划、托管层级和 SKU
104,900 周安装
services| array |
| Specific services to start |
| all services |
profiles | array | Profiles to activate | none |
watch | boolean | Enable Compose Watch mode | false |
since| string |
| Show logs since timestamp/duration |
| none |
project_name | string | Project name override | directory name |
project_name| string |
| Project name override |
| directory name |
env| object |
| Environment variables |
| none |
project_name | string | Project name override | directory name |
user| string |
| User to execute as |
| container default |
env | object | Environment variables | none |
project_name | string | Project name override | directory name |
;DOCKER_HOST | Docker daemon socket | unix:///var/run/docker.sock |
COMPOSE_HTTP_TIMEOUT | HTTP timeout for API calls | 60 |
COMPOSE_PARALLEL_LIMIT | Max parallel operations | unlimited |
| Port conflict |
| Change port mapping or stop conflicting service |
no configuration file provided | Missing compose file | Create compose.yaml |
service ... must be built | Image not built | Run docker compose build |
service unhealthy | Healthcheck failing | Check docker compose logs <service> |
include path not found | Missing included file | Verify paths in include: block |
depends_on: condition: service_healthyExposing all ports to host (0.0.0.0) | Services accessible from any network interface including public interfaces | Bind to 127.0.0.1 for dev; use internal networks for service-to-service communication |
| No restart policy | Containers stay down after crash or host reboot in production | Use restart: unless-stopped for services that should auto-recover |