docker-init
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDocker 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:
- for Dockerfile patterns
02-dockerfile.md - for compose structure
03-compose-fundamentals.md - if database needed
05-databases.md - for folder structure
10-architecture.md
阅读相关文档:
- 用于Dockerfile编写模式
02-dockerfile.md - 用于compose结构
03-compose-fundamentals.md - (若需要数据库)
05-databases.md - 用于文件夹结构
10-architecture.md
3. Generate Files
3. 生成文件
Dockerfile
Dockerfile
dockerfile
undefineddockerfile
undefinedMulti-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 configurationKey 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
*.lognode_modules/
.git/
.env
*.log.env.example
.env.example
bash
undefinedbash
undefinedApplication
Application
NODE_ENV=development
PORT=3000
NODE_ENV=development
PORT=3000
Database
Database
DB_HOST=db
DB_USER=appuser
DB_PASSWORD=
undefinedDB_HOST=db
DB_USER=appuser
DB_PASSWORD=
undefined4. 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 /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 /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 /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 /app/main .
EXPOSE 8080
CMD ["./main"]Output
输出内容
Generated files:
Dockerfile- (or
docker-compose.yaml)compose.yaml .dockerignore.env.example- folder for additional configs (if needed)
docker/
生成的文件包括:
Dockerfile- (或
docker-compose.yaml)compose.yaml .dockerignore.env.example- 文件夹(用于存放额外配置,如有需要)
docker/