home / skills / ntaksh42 / agents / dockerfile-generator

dockerfile-generator skill

/.claude/skills/dockerfile-generator

This skill generates optimized multi-stage Dockerfiles with secure defaults and language-specific best practices to streamline containerization.

npx playbooks add skill ntaksh42/agents --skill dockerfile-generator

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.7 KB
---
name: dockerfile-generator
description: Generate optimized Dockerfiles with multi-stage builds and best practices. Use when containerizing applications or creating Docker configurations.
---

# Dockerfile Generator Skill

最適化されたDockerfileを生成するスキルです。

## 概要

プロジェクトの種類に応じた、セキュアで効率的なDockerfileを自動生成します。

## 主な機能

- **マルチステージビルド**: イメージサイズ削減
- **セキュリティ**: 非rootユーザー、最小権限
- **キャッシュ最適化**: ビルド時間短縮
- **ベストプラクティス**: Docker推奨設定
- **複数言語対応**: Node.js、Python、Go、Java等

## 生成例

### Node.js (Express)

```dockerfile
# マルチステージビルド
FROM node:18-alpine AS builder

WORKDIR /app

# 依存関係のみ先にコピー(キャッシュ最適化)
COPY package*.json ./
RUN npm ci --only=production

# ソースコードコピー
COPY . .

# 本番イメージ
FROM node:18-alpine

# 非rootユーザー作成
RUN addgroup -g 1001 -S nodejs && \
    adduser -S nodejs -u 1001

WORKDIR /app

# ビルド成果物をコピー
COPY --from=builder --chown=nodejs:nodejs /app/node_modules ./node_modules
COPY --chown=nodejs:nodejs . .

USER nodejs

EXPOSE 3000

CMD ["node", "server.js"]
```

### Python (FastAPI)

```dockerfile
FROM python:3.11-slim AS builder

WORKDIR /app

# システム依存関係
RUN apt-get update && \
    apt-get install -y --no-install-recommends gcc && \
    rm -rf /var/lib/apt/lists/*

# Python依存関係
COPY requirements.txt .
RUN pip install --user --no-cache-dir -r requirements.txt

# 本番イメージ
FROM python:3.11-slim

RUN adduser --disabled-password --gecos '' appuser

WORKDIR /app

# ビルドしたパッケージをコピー
COPY --from=builder /root/.local /home/appuser/.local
COPY --chown=appuser:appuser . .

USER appuser

ENV PATH=/home/appuser/.local/bin:$PATH

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
```

### Go

```dockerfile
# ビルドステージ
FROM golang:1.21-alpine AS builder

WORKDIR /app

# 依存関係
COPY go.mod go.sum ./
RUN go mod download

# ビルド
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# 最小イメージ
FROM scratch

COPY --from=builder /app/main /main

EXPOSE 8080

ENTRYPOINT ["/main"]
```

### React (静的サイト)

```dockerfile
# ビルドステージ
FROM node:18-alpine AS builder

WORKDIR /app

COPY package*.json ./
RUN npm ci

COPY . .
RUN npm run build

# Nginxで配信
FROM nginx:alpine

COPY --from=builder /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
```

## .dockerignore

```
node_modules
npm-debug.log
.git
.gitignore
README.md
.env
.DS_Store
*.log
dist
build
coverage
```

## docker-compose.yml

```yaml
version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    volumes:
      - ./data:/app/data
    restart: unless-stopped

  database:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped

volumes:
  postgres_data:
```

## ベストプラクティス

1. **マルチステージビルド**: イメージサイズ削減
2. **レイヤーキャッシュ**: 変更の少ないファイルから順に
3. **非rootユーザー**: セキュリティ向上
4. **.dockerignore**: 不要ファイル除外
5. **ヘルスチェック**: コンテナ監視

## バージョン情報

- スキルバージョン: 1.0.0

Overview

This skill generates optimized Dockerfiles tailored to your project type, applying multi-stage builds, security defaults, and cache-friendly ordering. It produces production-ready configurations for Node.js, Python, Go, Java, static sites and more. Outputs include a .dockerignore template and optional docker-compose service scaffolding. The goal is smaller images, faster builds, and safer containers.

How this skill works

Provide project details (language, build steps, ports, required runtime tools) and the generator creates a Dockerfile using multi-stage builds when appropriate. It enforces non-root users, minimal base images, and layered copy/install ordering to maximize cache hits. The skill also emits a recommended .dockerignore and a sample docker-compose.yml to simplify local development and deployment.

When to use it

  • Containerizing a new application for production deployment
  • Converting local builds into reproducible Docker images
  • Reducing image size with multi-stage builds
  • Applying Docker best practices for security and caching
  • Creating a starting Dockerfile for CI/CD pipelines

Best practices

  • Use multi-stage builds to separate build tooling from runtime artifacts
  • Order COPY and install steps so dependencies change less often to leverage layer cache
  • Run containers as a non-root user and drop unnecessary capabilities
  • Keep final images minimal (alpine, scratch, or slim runtime images)
  • Include a .dockerignore to avoid sending dev files into the build context
  • Add HEALTHCHECK and explicit EXPOSE/CMD for better orchestration behavior

Example use cases

  • Generate a Node.js Dockerfile that caches npm installs and runs as a non-root user
  • Create a Python/FastAPI image that installs compiled dependencies in a builder stage
  • Produce a Go static binary image using scratch for minimal attack surface
  • Build a React static site pipeline that outputs to nginx for static hosting
  • Scaffold a docker-compose.yml for local development with a linked Postgres service

FAQ

Can the generator target slim or alpine base images?

Yes. You can choose slim, alpine, or scratch runtimes depending on compatibility and size goals; the generator adapts install steps accordingly.

How does it improve build caching?

It orders steps to copy dependency manifests first and run installs before copying source files so unchanged dependencies reuse cached layers.

Will it add a non-root user by default?

Yes. The generated Dockerfiles create and switch to a non-root user in the final image to follow security best practices.