Dockerセキュリティ完全ガイド
Dockerセキュリティ完全ガイド
Section titled “Dockerセキュリティ完全ガイド”Dockerコンテナのセキュリティ対策を、実務で使える実装例とともに詳しく解説します。
1. Dockerセキュリティとは
Section titled “1. Dockerセキュリティとは”セキュリティの重要性
Section titled “セキュリティの重要性”コンテナのセキュリティは、アプリケーション全体のセキュリティに直接影響します。適切な対策により、脆弱性から保護できます。
セキュリティ対策 ├─ 非rootユーザーでの実行 ├─ イメージのスキャン ├─ ネットワークの分離 └─ シークレットの管理2. 非rootユーザーでの実行
Section titled “2. 非rootユーザーでの実行”非rootユーザーの作成
Section titled “非rootユーザーの作成”# DockerfileFROM node:18-alpine
# 非rootユーザーを作成RUN addgroup -g 1001 -S nodejs && \ adduser -S nextjs -u 1001
WORKDIR /app
# ファイルの所有権を変更COPY --chown=nextjs:nodejs . .
# 非rootユーザーに切り替えUSER nextjs
CMD ["node", "index.js"]権限の最小化
Section titled “権限の最小化”FROM node:18-alpine
# 必要な権限のみ付与RUN chmod 755 /app && \ chown -R nextjs:nodejs /app
USER nextjs3. イメージのスキャン
Section titled “3. イメージのスキャン”Trivyを使用したスキャン
Section titled “Trivyを使用したスキャン”# Trivyのインストールbrew install trivy # macOS# またはsudo apt-get install trivy # Linux
# イメージのスキャンtrivy image my-app:latest
# 特定の深刻度のみ表示trivy image --severity HIGH,CRITICAL my-app:latestDocker Scoutの使用
Section titled “Docker Scoutの使用”# Docker Scoutでスキャンdocker scout quickview my-app:latest
# 詳細なレポートdocker scout cves my-app:latest4. シークレットの管理
Section titled “4. シークレットの管理”環境変数での管理
Section titled “環境変数での管理”services: app: build: . environment: - DATABASE_PASSWORD=${DB_PASSWORD} # 環境変数から取得 env_file: - .envDocker Secretsの使用
Section titled “Docker Secretsの使用”version: '3.8'
services: app: build: . secrets: - db_password environment: - DB_PASSWORD_FILE=/run/secrets/db_password
secrets: db_password: file: ./secrets/db_password.txt5. ネットワークの分離
Section titled “5. ネットワークの分離”プライベートネットワーク
Section titled “プライベートネットワーク”version: '3.8'
services: app: build: . networks: - public-network
db: image: postgres:14-alpine networks: - private-network # 外部からアクセス不可
networks: public-network: driver: bridge private-network: driver: bridge internal: true # 外部アクセスを無効化6. リソース制限
Section titled “6. リソース制限”CPUとメモリの制限
Section titled “CPUとメモリの制限”services: app: build: . deploy: resources: limits: cpus: '0.5' memory: 512M reservations: cpus: '0.25' memory: 256Mコンテナのリソース制限
Section titled “コンテナのリソース制限”# CPU制限docker run --cpus="0.5" my-app:latest
# メモリ制限docker run --memory="512m" my-app:latest
# 両方の制限docker run --cpus="0.5" --memory="512m" my-app:latest7. 実務でのベストプラクティス
Section titled “7. 実務でのベストプラクティス”パターン1: 最小権限の原則
Section titled “パターン1: 最小権限の原則”FROM node:18-alpine
# 必要なパッケージのみインストールRUN apk add --no-cache \ curl \ && rm -rf /var/cache/apk/*
# 不要なパッケージを削除RUN apk del curlパターン2: イメージの署名
Section titled “パターン2: イメージの署名”# イメージの署名docker trust sign my-app:latest
# 署名の検証docker trust inspect my-app:latest8. よくある問題と解決策
Section titled “8. よくある問題と解決策”問題1: コンテナがrootで実行されている
Section titled “問題1: コンテナがrootで実行されている”原因:
- USERディレクティブが設定されていない
解決策:
# 非rootユーザーを作成して使用RUN adduser -S appuserUSER appuser問題2: 脆弱性が検出される
Section titled “問題2: 脆弱性が検出される”原因:
- 古いベースイメージを使用している
- 依存関係に脆弱性がある
解決策:
# イメージをスキャンtrivy image my-app:latest
# ベースイメージを更新FROM node:18-alpine # 最新版を使用
# 依存関係を更新npm audit fixこれで、Dockerセキュリティの基礎知識と実務での使い方を理解できるようになりました。