docker-expert

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Docker Expert

Docker 专家

Overview

概述

This skill enables AI assistants to help with Docker containerization, docker-compose setups, and container orchestration for micro startup infrastructure.
此技能可让AI助手协助完成针对初创企业基础设施的Docker容器化、docker-compose搭建以及容器编排工作。

When to Use This Skill

何时使用此技能

This skill activates when users need:
  • Dockerfile creation for NestJS/Next.js
  • docker-compose configuration
  • Container networking and volumes
  • Multi-stage builds optimization
  • Health checks and restart policies
  • MongoDB/Redis container setup
当用户需要以下服务时,可激活此技能:
  • 为NestJS/Next.js创建Dockerfile
  • docker-compose配置
  • 容器网络与卷管理
  • 多阶段构建优化
  • 健康检查与重启策略
  • MongoDB/Redis容器搭建

Dockerfile Best Practices

Dockerfile 最佳实践

NestJS Multi-Stage Build

NestJS 多阶段构建

dockerfile
FROM node:20-alpine AS base
WORKDIR /app

FROM base AS deps
COPY package*.json ./
RUN npm ci

FROM base AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM base AS runner
ENV NODE_ENV=production
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3001
CMD ["node", "dist/main.js"]
dockerfile
FROM node:20-alpine AS base
WORKDIR /app

FROM base AS deps
COPY package*.json ./
RUN npm ci

FROM base AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM base AS runner
ENV NODE_ENV=production
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3001
CMD ["node", "dist/main.js"]

Next.js Dockerfile

Next.js Dockerfile

dockerfile
FROM node:20-alpine AS base
WORKDIR /app

FROM base AS deps
COPY package*.json ./
RUN npm ci

FROM base AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM base AS runner
ENV NODE_ENV=production
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
EXPOSE 3000
CMD ["npm", "start"]
dockerfile
FROM node:20-alpine AS base
WORKDIR /app

FROM base AS deps
COPY package*.json ./
RUN npm ci

FROM base AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM base AS runner
ENV NODE_ENV=production
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
EXPOSE 3000
CMD ["npm", "start"]

Docker Compose Patterns

Docker Compose 模式

Development Setup

开发环境配置

  • Use volumes for live reload
  • Mount source code
  • Set restart: unless-stopped
  • Configure networks
  • 使用卷实现热重载
  • 挂载源代码
  • 设置restart: unless-stopped
  • 配置网络

Production Setup

生产环境配置

  • Use named volumes for persistence
  • Set restart policies
  • Configure health checks
  • Use secrets management
  • 使用命名卷实现数据持久化
  • 设置重启策略
  • 配置健康检查
  • 使用密钥管理

MongoDB with Docker Compose

MongoDB 与 Docker Compose

yaml
services:
  mongodb:
    image: mongo:7.0
    container_name: mongodb
    restart: unless-stopped
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ROOT_PASSWORD}
    volumes:
      - mongodb_data:/data/db
    networks:
      - app-network
    command: mongod --auth
yaml
services:
  mongodb:
    image: mongo:7.0
    container_name: mongodb
    restart: unless-stopped
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ROOT_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ROOT_PASSWORD}
    volumes:
      - mongodb_data:/data/db
    networks:
      - app-network
    command: mongod --auth

Health Checks

健康检查

yaml
healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:3001/health"]
  interval: 30s
  timeout: 10s
  retries: 3
  start_period: 40s
yaml
healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:3001/health"]
  interval: 30s
  timeout: 10s
  retries: 3
  start_period: 40s

Best Practices

最佳实践

  • Use multi-stage builds to reduce image size
  • Leverage layer caching
  • Use .dockerignore
  • Set appropriate restart policies
  • Use health checks for containers
  • Mount volumes for persistent data
  • Use networks for service isolation
  • 使用多阶段构建减小镜像体积
  • 利用分层缓存
  • 使用.dockerignore文件
  • 设置合适的重启策略
  • 为容器配置健康检查
  • 挂载卷实现数据持久化
  • 使用网络实现服务隔离