docker-init

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Docker Environment Initialization Skill

Docker环境初始化技能

Overview

概述

This skill creates a complete Docker environment for a project, including:
  • Dockerfile optimized for the application type
  • docker-compose.yaml with all required services
  • .dockerignore for efficient builds
  • .env.example for environment configuration
本技能可为项目创建完整的Docker环境,包括:
  • 针对应用类型优化的Dockerfile
  • 包含所有必要服务的docker-compose.yaml
  • 用于高效构建的.dockerignore
  • 用于环境配置的.env.example

Activation

适用场景

Use this skill when:
  • Setting up Docker for a new project
  • Migrating an existing project to Docker
  • Adding Docker support to a codebase
在以下场景中使用本技能:
  • 为新项目搭建Docker环境
  • 将现有项目迁移至Docker
  • 为代码库添加Docker支持

Process

操作流程

1. Detect Project Type

1. 检测项目类型

Analyze the project to determine:
  • Primary language (Node.js, Python, PHP, Go, etc.)
  • Framework (Laravel, Express, Django, etc.)
  • Required services (database, cache, queue)
  • Existing configuration files
分析项目以确定:
  • 主要编程语言(Node.js、Python、PHP、Go等)
  • 框架(Laravel、Express、Django等)
  • 所需服务(数据库、缓存、队列)
  • 现有配置文件

2. Consult Documentation

2. 查阅文档

Read relevant documentation:
  • 02-dockerfile.md
    for Dockerfile patterns
  • 03-compose-fundamentals.md
    for compose structure
  • 05-databases.md
    if database needed
  • 10-architecture.md
    for folder structure
阅读相关文档:
  • 02-dockerfile.md
    用于Dockerfile编写模式
  • 03-compose-fundamentals.md
    用于compose结构
  • 05-databases.md
    (若需要数据库)
  • 10-architecture.md
    用于文件夹结构

3. Generate Files

3. 生成文件

Dockerfile

Dockerfile

dockerfile
undefined
dockerfile
undefined

Multi-stage build pattern

Multi-stage build pattern

FROM base AS builder
FROM base AS builder

Build steps

Build steps

FROM base AS production
FROM base AS production

Production setup

Production setup


Key elements:
- Use appropriate base image
- Multi-stage build for smaller images
- Non-root user for security
- Health check
- Proper COPY order for caching

核心要素:
- 使用合适的基础镜像
- 多阶段构建以减小镜像体积
- 使用非root用户保障安全
- 健康检查
- 合理的COPY顺序以利用缓存

docker-compose.yaml

docker-compose.yaml

yaml
services:
  app:
    build: .
    # Configuration

  db:
    image: postgres:16
    # Configuration

volumes:
  # Named volumes

networks:
  # Network configuration
Key elements:
  • No version field (modern compose)
  • Health checks
  • Dependencies with conditions
  • Named volumes
  • Proper networking
yaml
services:
  app:
    build: .
    # Configuration

  db:
    image: postgres:16
    # Configuration

volumes:
  # Named volumes

networks:
  # Network configuration
核心要素:
  • 无version字段(现代compose)
  • 健康检查
  • 带条件的依赖配置
  • 命名卷
  • 合理的网络配置

.dockerignore

.dockerignore

node_modules/
.git/
.env
*.log
node_modules/
.git/
.env
*.log

.env.example

.env.example

bash
undefined
bash
undefined

Application

Application

NODE_ENV=development PORT=3000
NODE_ENV=development PORT=3000

Database

Database

DB_HOST=db DB_USER=appuser DB_PASSWORD=
undefined
DB_HOST=db DB_USER=appuser DB_PASSWORD=
undefined

4. Provide Instructions

4. 提供操作说明

Include:
  • How to start services
  • Available commands
  • Environment setup
  • Development workflow
包含:
  • 如何启动服务
  • 可用命令
  • 环境设置
  • 开发工作流

Templates by Project Type

按项目类型划分的模板

Node.js

Node.js

dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
USER node
EXPOSE 3000
CMD ["node", "src/index.js"]
dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
USER node
EXPOSE 3000
CMD ["node", "src/index.js"]

Python

Python

dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER nobody
EXPOSE 8000
CMD ["python", "app.py"]
dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER nobody
EXPOSE 8000
CMD ["python", "app.py"]

PHP/Laravel

PHP/Laravel

dockerfile
FROM php:8.3-fpm-alpine
WORKDIR /var/www/html
RUN apk add --no-cache postgresql-dev && \
    docker-php-ext-install pdo pdo_pgsql
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . .
RUN composer install --no-dev --optimize-autoloader
EXPOSE 9000
CMD ["php-fpm"]
dockerfile
FROM php:8.3-fpm-alpine
WORKDIR /var/www/html
RUN apk add --no-cache postgresql-dev && \
    docker-php-ext-install pdo pdo_pgsql
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY . .
RUN composer install --no-dev --optimize-autoloader
EXPOSE 9000
CMD ["php-fpm"]

Go

Go

dockerfile
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.* ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o main .

FROM alpine:latest
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]
dockerfile
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.* ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o main .

FROM alpine:latest
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]

Output

输出内容

Generated files:
  • Dockerfile
  • docker-compose.yaml
    (or
    compose.yaml
    )
  • .dockerignore
  • .env.example
  • docker/
    folder for additional configs (if needed)
生成的文件包括:
  • Dockerfile
  • docker-compose.yaml
    (或
    compose.yaml
  • .dockerignore
  • .env.example
  • docker/
    文件夹(用于存放额外配置,如有需要)