跳到正文
cccskills
免费在 GitHub 公开

docker-best-practices

Docker best practices including multi-stage builds, compose patterns, image optimization, and security

中文简介正在准备中。当前显示原文说明。

集成工具DockerPostgreSQL
查看安装方法

包含的文件(1)

  • SKILL.md3.7 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Docker Best Practices

Multi-Stage Build

FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --only=production

FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-alpine AS runtime
WORKDIR /app
RUN addgroup -g 1001 -S appgroup && adduser -S appuser -u 1001 -G appgroup
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
COPY --from=build /app/package.json ./
USER appuser
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://localhost:3000/healthz || exit 1
CMD ["node", "dist/server.js"]

Separate dependency installation from build steps. Final stage contains only runtime artifacts.

Python Multi-Stage

FROM python:3.12-slim AS builder
WORKDIR /app
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

FROM python:3.12-slim
WORKDIR /app
RUN useradd --create-home appuser
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY . .
USER appuser
CMD ["gunicorn", "app:create_app()", "-b", "0.0.0.0:8000", "-w", "4"]

Docker Compose

services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
      target: runtime
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/app
      - REDIS_URL=redis://cache:6379
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started
    restart: unless-stopped
    deploy:
      resources:
        limits:
          memory: 512M

  db:
    image: postgres:16-alpine
    volumes:
      - pgdata:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U user -d app"]
      interval: 5s
      timeout: 3s
      retries: 5

  cache:
    image: redis:7-alpine
    command: redis-server --maxmemory 128mb --maxmemory-policy allkeys-lru

volumes:
  pgdata:

.dockerignore

node_modules
.git
.env*
*.md
docker-compose*.yml
.github
coverage
dist

Always include a .dockerignore to reduce build context size and prevent leaking secrets.

Image Optimization Tips

# Check image size breakdown
docker history --human --no-trunc <image>

# Use dive for layer analysis
dive <image>

# Multi-arch build
docker buildx build --platform linux/amd64,linux/arm64 -t registry/app:1.0 --push .

Combine RUN commands to reduce layers. Order instructions from least to most frequently changing for cache efficiency.

Anti-Patterns

  • Running as root inside containers
  • Using ADD when COPY suffices (ADD auto-extracts tarballs, pulls URLs)
  • Storing secrets in environment variables in Dockerfiles
  • Not pinning base image versions (FROM node:latest)
  • Missing .dockerignore causing large build contexts
  • Installing dev dependencies in production images

Checklist

  • Multi-stage build separates build and runtime stages
  • Non-root user created and used with USER directive
  • Base images pinned to specific versions (e.g., node:22-alpine)
  • .dockerignore excludes .git, node_modules, .env
  • HEALTHCHECK instruction defined
  • Production image contains no build tools or dev dependencies
  • docker-compose uses depends_on with health conditions
  • Secrets passed via build secrets or runtime mounts, not ENV in Dockerfile

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Web accessibility patterns for WCAG 2.2 compliance including ARIA, keyboard navigation, screen readers, and testing

中文简介正在准备中。当前显示原文说明。

rohitg00/awesome-claude-code-toolkit27182026年5月12日 更新

Route broad or ambiguous AgentKit SEO work to the right module while keeping context scoped. Use when a request spans multiple surfaces, asks for overall digital-presence strategy, involves provider or install architecture, needs agent-context planning, or the correct platform skill is unclear.

中文简介正在准备中。当前显示原文说明。

rohitg00/awesome-claude-code-toolkit27182026年5月12日 更新

REST API design with resource naming, pagination, versioning, and OpenAPI spec generation

中文简介正在准备中。当前显示原文说明。

rohitg00/awesome-claude-code-toolkit27182026年5月12日 更新

Authentication and authorization patterns including OAuth2, JWT, RBAC, session management, and PKCE flows

中文简介正在准备中。当前显示原文说明。

rohitg00/awesome-claude-code-toolkit27182026年5月12日 更新

AWS cloud patterns for Lambda, ECS, S3, DynamoDB, and Infrastructure as Code with CDK/Terraform

中文简介正在准备中。当前显示原文说明。

rohitg00/awesome-claude-code-toolkit27182026年5月12日 更新

CI/CD pipeline patterns for GitHub Actions, GitLab CI, testing strategies, and deployment automation

中文简介正在准备中。当前显示原文说明。

rohitg00/awesome-claude-code-toolkit27182026年5月12日 更新

查看 rohitg00 的全部技能

报告此技能的问题