docker-reviewer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Docker Reviewer Skill

Docker Reviewer Skill

Purpose

用途

Reviews Dockerfiles and docker-compose configurations for optimization, security, and best practices.
针对Dockerfile和docker-compose配置进行优化、安全及最佳实践审查。

When to Use

适用场景

  • Dockerfile code review
  • Docker image optimization
  • docker-compose.yml review
  • Container security audit
  • Build time optimization
  • Dockerfile代码审查
  • Docker镜像优化
  • docker-compose.yml配置审查
  • 容器安全审计
  • 构建时间优化

Project Detection

项目检测条件

  • Dockerfile
    in project
  • docker-compose.yml
    or
    docker-compose.yaml
  • .dockerignore
    file
  • Dockerfile.*
    variants
  • 项目中存在
    Dockerfile
    文件
  • 存在
    docker-compose.yml
    docker-compose.yaml
    文件
  • 存在
    .dockerignore
    文件
  • 存在
    Dockerfile.*
    变体文件

Workflow

工作流程

Step 1: Analyze Project

步骤1:分析项目

**Base Image**: node:20-alpine
**Build Type**: Multi-stage
**Compose**: v3.8
**Registry**: Docker Hub / ECR / GCR
**Base Image**: node:20-alpine
**Build Type**: Multi-stage
**Compose**: v3.8
**Registry**: Docker Hub / ECR / GCR

Step 2: Select Review Areas

步骤2:选择审查领域

AskUserQuestion:
"Which areas to review?"
Options:
- Full Docker review (recommended)
- Dockerfile optimization
- Layer caching strategy
- Security hardening
- docker-compose review
multiSelect: true
询问用户问题:
"需要审查哪些领域?"
选项:
- 完整Docker审查(推荐)
- Dockerfile优化
- 镜像层缓存策略
- 安全加固
- docker-compose配置审查
multiSelect: true

Detection Rules

检测规则

Image Optimization

镜像优化

CheckRecommendationSeverity
Large base imageUse alpine/slim/distrolessHIGH
No multi-stage buildAdd build stageMEDIUM
Too many layersCombine RUN commandsMEDIUM
Installing dev depsSeparate build/runtimeHIGH
dockerfile
undefined
检查项建议严重程度
基础镜像过大使用alpine/slim/distroless镜像
未使用多阶段构建添加构建阶段
镜像层过多合并RUN命令
安装了开发依赖分离构建阶段与运行阶段
dockerfile
undefined

BAD: Large image with dev dependencies

不良示例:包含开发依赖的大镜像

FROM node:20 WORKDIR /app COPY . . RUN npm install RUN npm run build CMD ["node", "dist/index.js"]
FROM node:20 WORKDIR /app COPY . . RUN npm install RUN npm run build CMD ["node", "dist/index.js"]

Result: ~1GB image

结果:约1GB镜像

GOOD: Multi-stage with alpine

良好示例:使用alpine的多阶段构建

FROM node:20-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build
FROM node:20-alpine AS runner WORKDIR /app ENV NODE_ENV=production COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules USER node CMD ["node", "dist/index.js"]
FROM node:20-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build
FROM node:20-alpine AS runner WORKDIR /app ENV NODE_ENV=production COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules USER node CMD ["node", "dist/index.js"]

Result: ~150MB image

结果:约150MB镜像

undefined
undefined

Layer Caching

镜像层缓存

CheckRecommendationSeverity
COPY . before installCopy package files firstHIGH
No .dockerignoreAdd .dockerignoreMEDIUM
Changing files earlyOrder by change frequencyMEDIUM
dockerfile
undefined
检查项建议严重程度
在安装依赖前复制全部文件先复制package相关文件
缺少.dockerignore文件添加.dockerignore文件
频繁变更的文件放在前面按变更频率排序文件复制顺序
dockerfile
undefined

BAD: Cache invalidation on every code change

不良示例:代码变更导致缓存失效

FROM node:20-alpine WORKDIR /app COPY . . # Invalidates cache on ANY change RUN npm install # Always reinstalls
FROM node:20-alpine WORKDIR /app COPY . . # 任何变更都会使缓存失效 RUN npm install # 每次都需要重新安装

GOOD: Leverage layer caching

良好示例:利用镜像层缓存

FROM node:20-alpine WORKDIR /app COPY package*.json ./ # Only invalidates on package change RUN npm ci # Cached if packages unchanged COPY . . # Code changes don't affect npm cache RUN npm run build

```gitignore
FROM node:20-alpine WORKDIR /app COPY package*.json ./ # 仅当依赖变更时缓存失效 RUN npm ci # 依赖未变更时复用缓存 COPY . . # 代码变更不影响npm缓存 RUN npm run build

```gitignore

.dockerignore

.dockerignore

node_modules .git .gitignore .md .env dist coverage .nyc_output
undefined
node_modules .git .gitignore .md .env dist coverage .nyc_output
undefined

Security

安全配置

CheckRecommendationSeverity
Running as rootAdd USER directiveCRITICAL
Latest tagPin specific versionHIGH
Secrets in buildUse build secretsCRITICAL
No health checkAdd HEALTHCHECKMEDIUM
dockerfile
undefined
检查项建议严重程度
以root用户运行添加USER指令严重
使用latest标签固定具体版本
构建过程中包含密钥使用构建密钥严重
缺少健康检查添加HEALTHCHECK
dockerfile
undefined

BAD: Security issues

不良示例:存在安全问题

FROM node:latest # Unpinned version WORKDIR /app COPY . . ENV API_KEY=secret123 # Secret in image! RUN npm install CMD ["node", "index.js"] # Running as root
FROM node:latest # 未固定版本 WORKDIR /app COPY . . ENV API_KEY=secret123 # 密钥被嵌入镜像! RUN npm install CMD ["node", "index.js"] # 以root用户运行

GOOD: Secure Dockerfile

良好示例:安全的Dockerfile

FROM node:20.10-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci --only=production
FROM node:20.10-alpine WORKDIR /app
FROM node:20.10-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci --only=production
FROM node:20.10-alpine WORKDIR /app

Create non-root user

创建非root用户

RUN addgroup -g 1001 appgroup &&
adduser -u 1001 -G appgroup -s /bin/sh -D appuser
COPY --from=builder --chown=appuser:appgroup /app/node_modules ./node_modules COPY --chown=appuser:appgroup . .
USER appuser
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3
CMD wget --quiet --tries=1 --spider http://localhost:3000/health || exit 1
EXPOSE 3000 CMD ["node", "index.js"]
undefined
RUN addgroup -g 1001 appgroup &&
adduser -u 1001 -G appgroup -s /bin/sh -D appuser
COPY --from=builder --chown=appuser:appgroup /app/node_modules ./node_modules COPY --chown=appuser:appgroup . .
USER appuser
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3
CMD wget --quiet --tries=1 --spider http://localhost:3000/health || exit 1
EXPOSE 3000 CMD ["node", "index.js"]
undefined

Build Secrets (Docker BuildKit)

构建密钥(Docker BuildKit)

dockerfile
undefined
dockerfile
undefined

syntax=docker/dockerfile:1.4

syntax=docker/dockerfile:1.4

FROM node:20-alpine WORKDIR /app
FROM node:20-alpine WORKDIR /app

Mount secret during build (not stored in layer)

构建时挂载密钥(不存储在镜像层中)

RUN --mount=type=secret,id=npm_token
NPM_TOKEN=$(cat /run/secrets/npm_token)
npm ci
RUN --mount=type=secret,id=npm_token
NPM_TOKEN=$(cat /run/secrets/npm_token)
npm ci

Build command:

构建命令:

DOCKER_BUILDKIT=1 docker build --secret id=npm_token,src=.npmrc .

DOCKER_BUILDKIT=1 docker build --secret id=npm_token,src=.npmrc .

undefined
undefined

RUN Optimization

RUN命令优化

CheckRecommendationSeverity
Multiple RUN for cleanupCombine in single RUNMEDIUM
No cleanup after installRemove cache in same layerMEDIUM
dockerfile
undefined
检查项建议严重程度
多次RUN命令用于清理合并为单个RUN命令
安装后未清理缓存在同一镜像层中清理缓存
dockerfile
undefined

BAD: Multiple layers, cache not cleaned

不良示例:多镜像层,未清理缓存

RUN apt-get update RUN apt-get install -y curl RUN apt-get clean
RUN apt-get update RUN apt-get install -y curl RUN apt-get clean

GOOD: Single layer with cleanup

良好示例:单镜像层并清理缓存

RUN apt-get update &&
apt-get install -y --no-install-recommends curl &&
apt-get clean &&
rm -rf /var/lib/apt/lists/*
undefined
RUN apt-get update &&
apt-get install -y --no-install-recommends curl &&
apt-get clean &&
rm -rf /var/lib/apt/lists/*
undefined

Docker Compose

Docker Compose配置

CheckRecommendationSeverity
No resource limitsAdd deploy.resourcesHIGH
No health checksAdd healthcheckMEDIUM
Hardcoded configUse environment variablesMEDIUM
No restart policyAdd restart: unless-stoppedMEDIUM
yaml
undefined
检查项建议严重程度
未设置资源限制添加deploy.resources配置
缺少健康检查添加healthcheck配置
硬编码配置使用环境变量
未设置重启策略添加restart: unless-stopped配置
yaml
undefined

BAD: Minimal compose

不良示例:极简配置

version: '3.8' services: app: build: . ports: - "3000:3000" db: image: postgres environment: POSTGRES_PASSWORD: password123
version: '3.8' services: app: build: . ports: - "3000:3000" db: image: postgres environment: POSTGRES_PASSWORD: password123

GOOD: Production-ready compose

良好示例:生产就绪配置

version: '3.8'
services: app: build: context: . dockerfile: Dockerfile ports: - "3000:3000" environment: - NODE_ENV=production - DATABASE_URL=postgresql://user:${DB_PASSWORD}@db:5432/app depends_on: db: condition: service_healthy restart: unless-stopped deploy: resources: limits: cpus: '1' memory: 512M reservations: cpus: '0.5' memory: 256M healthcheck: test: ["CMD", "wget", "-q", "--spider", "http://localhost:3000/health"] interval: 30s timeout: 10s retries: 3 start_period: 40s
db: image: postgres:15-alpine environment: POSTGRES_USER: user POSTGRES_PASSWORD: ${DB_PASSWORD} POSTGRES_DB: app volumes: - postgres_data:/var/lib/postgresql/data restart: unless-stopped healthcheck: test: ["CMD-SHELL", "pg_isready -U user -d app"] interval: 10s timeout: 5s retries: 5
volumes: postgres_data:
undefined
version: '3.8'
services: app: build: context: . dockerfile: Dockerfile ports: - "3000:3000" environment: - NODE_ENV=production - DATABASE_URL=postgresql://user:${DB_PASSWORD}@db:5432/app depends_on: db: condition: service_healthy restart: unless-stopped deploy: resources: limits: cpus: '1' memory: 512M reservations: cpus: '0.5' memory: 256M healthcheck: test: ["CMD", "wget", "-q", "--spider", "http://localhost:3000/health"] interval: 30s timeout: 10s retries: 3 start_period: 40s
db: image: postgres:15-alpine environment: POSTGRES_USER: user POSTGRES_PASSWORD: ${DB_PASSWORD} POSTGRES_DB: app volumes: - postgres_data:/var/lib/postgresql/data restart: unless-stopped healthcheck: test: ["CMD-SHELL", "pg_isready -U user -d app"] interval: 10s timeout: 5s retries: 5
volumes: postgres_data:
undefined

Response Template

响应模板

undefined
undefined

Docker Review Results

Docker审查结果

Project: [name] Base Image: node:20-alpine Build: Multi-stage | Compose: v3.8
项目: [name] 基础镜像: node:20-alpine 构建方式: 多阶段构建 | Compose版本: v3.8

Image Optimization

镜像优化

StatusFileIssue
HIGHDockerfileUsing node:latest (~1GB)
状态文件问题
Dockerfile使用node:latest(约1GB)

Layer Caching

镜像层缓存

StatusFileIssue
HIGHDockerfile:5COPY . before npm install
状态文件问题
Dockerfile:5在npm install前复制了全部文件

Security

安全配置

StatusFileIssue
CRITICALDockerfileRunning as root user
状态文件问题
严重Dockerfile以root用户运行

Compose

Compose配置

StatusFileIssue
HIGHdocker-compose.ymlNo resource limits
状态文件问题
docker-compose.yml未设置资源限制

Recommended Actions

建议操作

  1. Switch to node:20-alpine base image
  2. Add multi-stage build
  3. Add USER directive for non-root
  4. Add resource limits in compose
undefined
  1. 切换为node:20-alpine基础镜像
  2. 添加多阶段构建
  3. 添加USER指令使用非root用户
  4. 在Compose中添加资源限制
undefined

Best Practices

最佳实践

  1. Base Image: Use alpine/slim/distroless
  2. Multi-stage: Separate build and runtime
  3. Caching: Order by change frequency
  4. Security: Non-root, pinned versions, no secrets
  5. Compose: Health checks, resource limits
  1. 基础镜像:使用alpine/slim/distroless镜像
  2. 多阶段构建:分离构建阶段与运行阶段
  3. 缓存策略:按变更频率排序文件复制顺序
  4. 安全配置:使用非root用户、固定版本、不包含密钥
  5. Compose配置:添加健康检查、资源限制

Integration

集成工具

  • k8s-reviewer
    : Kubernetes deployments
  • security-scanner
    : Container security
  • ci-cd-reviewer
    : Build pipelines
  • k8s-reviewer
    :Kubernetes部署配置审查
  • security-scanner
    :容器安全扫描
  • ci-cd-reviewer
    :构建流水线审查