Docker expert

From findscripter/everything-skills · View interactive version

Docker expert is an AI agent skill from the GitHub repository findscripter/everything-skills. 当排查 Docker 构建/镜像/编排/安全问题时使用;做镜像瘦身、多阶段构建、安全加固、Compose 编排与生产部署的诊断与重写,产出优化后的 Dockerfile/Compose 与评审清单;不适用于 K8s 编排、CI/CD 流水线、云厂商容器服务(ECS/Fargate)等深度场景;触发词:Dockerfile、镜像瘦身、多阶段构建、Compose、容器安全、distroless

How to use this skill

Skill file

何时使用

适用于围绕 Docker 容器的优化、加固与编排问题:

不该用(应转交他人):

遇到上述场景应明确建议切换并停止,例如:「这属于 Kubernetes 编排范畴,建议改用 kubernetes 专家。这里先停。」

步骤

  1. 优先用内置工具勘察(Read / Grep / Glob 性能更好,shell 命令仅作兜底)。 检测 Docker 环境与项目结构:
# 环境检测
docker --version 2>/dev/null || echo "未安装 Docker"
docker info | grep -E "Server Version|Storage Driver|Container Runtime" 2>/dev/null

# 项目结构
find . -name "Dockerfile*" -type f | head -10
find . -name "*compose*.yml" -o -name "*compose*.yaml" -type f | head -5
find . -name ".dockerignore" -type f | head -3

# 运行中状态
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}" 2>/dev/null | head -10
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" 2>/dev/null | head -10
  1. 顺应现有约定:匹配已有 Dockerfile 模式与基础镜像、尊重已有多阶段约定、区分开发与生产环境、考虑已有编排(Compose/Swarm)。
  1. 定位问题类别与复杂度,从下方核心领域选取对应策略。
  1. 彻底验证(改完必做):
# 构建与安全
docker build --no-cache -t test-build . 2>/dev/null && echo "构建成功"
docker history test-build --no-trunc 2>/dev/null | head -5
docker scout quickview test-build 2>/dev/null || echo "无 Docker Scout"

# 运行期
docker run --rm -d --name validation-test test-build 2>/dev/null
docker exec validation-test ps aux 2>/dev/null | head -3
docker stop validation-test 2>/dev/null

# Compose 校验
docker-compose config 2>/dev/null && echo "Compose 配置有效"

指令

按问题类别套用对应策略:

示例

优化的多阶段构建(含非 root、健康检查):

FROM node:18-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production && npm cache clean --force

FROM node:18-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --production

FROM node:18-alpine AS runtime
RUN addgroup -g 1001 -S nodejs && adduser -S nextjs -u 1001
WORKDIR /app
COPY --from=deps --chown=nextjs:nodejs /app/node_modules ./node_modules
COPY --from=build --chown=nextjs:nodejs /app/dist ./dist
COPY --from=build --chown=nextjs:nodejs /app/package*.json ./
USER nextjs
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:3000/health || exit 1
CMD ["node", "dist/index.js"]

最小化生产镜像(distroless):

FROM gcr.io/distroless/nodejs18-debian11
COPY --from=build /app/dist /app
COPY --from=build /app/node_modules /app/node_modules
WORKDIR /app
EXPOSE 3000
CMD ["index.js"]

生产级 Compose(健康检查 + 网络隔离 + 资源限制 + 外部密钥):

version: '3.8'
services:
  app:
    build:
      context: .
      target: 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
      start_period: 40s
    deploy:
      resources:
        limits:   { cpus: '0.5', memory: 512M }
        reservations: { cpus: '0.25', memory: 256M }
  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB_FILE: /run/secrets/db_name
      POSTGRES_USER_FILE: /run/secrets/db_user
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    secrets: [db_name, db_user, db_password]
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks: [backend]
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5
networks:
  frontend: { driver: bridge }
  backend:  { driver: bridge, internal: true }
volumes:
  postgres_data:
secrets:
  db_name:     { external: true }
  db_user:     { external: true }
  db_password: { external: true }

进阶片段:

# 多架构构建
docker buildx create --name multiarch-builder --use
docker buildx build --platform linux/amd64,linux/arm64 -t myapp:latest --push .
# 包管理器缓存挂载(BuildKit)
RUN --mount=type=cache,target=/root/.npm npm ci --only=production
# 构建期密钥(BuildKit),绝不落层
RUN --mount=type=secret,id=api_key \
    API_KEY=$(cat /run/secrets/api_key) && echo use-it

注意事项

评审清单(核心约束):

常见问题速诊:

通用底线: 输出不能替代针对具体环境的验证、测试与专家评审;缺少必要输入、权限、安全边界或成功标准时,停下来澄清。

互见


采编自 sickn33/antigravity-awesome-skills(MIT 许可)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.