DEV Community

myougaTheAxo
myougaTheAxo

Posted on • Originally published at zenn.dev

Claude CodeでDockerコンテナセキュリティを設計する:イメージスキャン・非root・最小権限

はじめに

Dockerイメージに脆弱性が入り込むのを防ぐ——Trivy・Dockerの非rootユーザー・読み取り専用ファイルシステムでコンテナのセキュリティを強化する。Claude Codeに設計を生成させる。


CLAUDE.mdにコンテナセキュリティ設計ルールを書く

## Dockerコンテナセキュリティ設計ルール

### Dockerfile
- ベースイメージ: node:22-alpine(最小サイズ)
- 非rootユーザー: USER node(UID 1000)
- 読み取り専用ファイルシステム: --read-only起動
- Multi-stage build: 開発依存を本番に含めない

### イメージスキャン
- CIでTrivyスキャン: HIGH/CRITICAL脆弱性でブロック
- ベースイメージは週次で更新(自動PR)
- Dockerfileのベストプラクティス: Hadolintでリント

### Runtime設定
- Capabilities: --cap-drop ALL(全capabilities削除)
- no-new-privileges: --security-opt no-new-privileges
- Seccomp: Dockerデフォルトプロファイルを適用
Enter fullscreen mode Exit fullscreen mode

コンテナセキュリティの生成

Node.jsアプリのセキュアなDockerfile設定を設計してください。

要件:
- Multi-stage build
- 非rootユーザー
- 脆弱性スキャン(Trivy)
- CI/CDパイプライン統合
- Runtime最小権限

生成ファイル: Dockerfile, .github/workflows/
Enter fullscreen mode Exit fullscreen mode

生成されるセキュアなDockerfile

# Dockerfile — セキュアなNode.js本番イメージ

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

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

FROM node:22-alpine AS production
WORKDIR /app

RUN apk upgrade --no-cache

COPY --from=deps   /app/node_modules ./node_modules
COPY --from=builder /app/dist        ./dist
COPY --from=builder /app/package.json ./

RUN chown -R node:node /app
USER node

VOLUME ["/app/tmp"]
EXPOSE 3000

HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
  CMD node -e "require('http').get('http://localhost:3000/health', r => process.exit(r.statusCode === 200 ? 0 : 1))"

CMD ["node", "dist/server.js"]
Enter fullscreen mode Exit fullscreen mode
# .github/workflows/container-security.yml

name: Container Security Scan

on:
  push:
    paths:
      - 'Dockerfile'
      - 'package*.json'
      - 'src/**'

jobs:
  security-scan:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Build Docker image
        run: docker build -t myapp:${{ github.sha }} .

      - name: Run Trivy vulnerability scan
        uses: aquasecurity/trivy-action@master
        with:
          image-ref: myapp:${{ github.sha }}
          format: 'sarif'
          output: 'trivy-results.sarif'
          exit-code: '1'
          ignore-unfixed: true
          severity: 'HIGH,CRITICAL'

      - name: Upload Trivy results to GitHub Security
        uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: 'trivy-results.sarif'

      - name: Lint Dockerfile with Hadolint
        uses: hadolint/hadolint-action@v3.1.0
        with:
          dockerfile: Dockerfile
          failure-threshold: warning
Enter fullscreen mode Exit fullscreen mode
# docker-compose.yml — 本番セキュリティ設定

services:
  app:
    image: myapp:latest
    user: "1000:1000"
    read_only: true
    tmpfs:
      - /tmp:size=100m
      - /app/tmp:size=100m
    cap_drop:
      - ALL
    security_opt:
      - no-new-privileges:true
    environment:
      NODE_ENV: production
    ports:
      - "3000:3000"
    restart: unless-stopped
Enter fullscreen mode Exit fullscreen mode

まとめ

Claude CodeでDockerコンテナセキュリティを設計する:

  1. CLAUDE.md にalpine使用・非rootユーザー・読み取り専用FS・CIでTrivyスキャンを明記
  2. Multi-stage build で本番イメージから開発依存・ソースを除外(イメージサイズ最小化)
  3. cap_drop ALL で全Linux Capabilitiesを削除(必要なものだけをcap_addで追加)
  4. 週次ベースイメージ自動PR でセキュリティパッチを継続的に適用

コンテナセキュリティ設計のレビューは **Security Pack(¥1,480)* の /security-check で確認できます。*

prompt-works.jp

Top comments (0)