Docker Composeファイル用のYAML Schemaを生成する

Docker Compose JSONをYAML Schema定義に変換する方法を学びます。サービス定義、ボリュームマウント、ネットワーク設定、環境変数、ヘルスチェック検証を解説します。

Real-World Configs

詳細な説明

Docker Compose設定からYAML Schemaへ

Docker Composeファイルはマルチコンテナアプリケーションを定義します。サンプルの構成をYAML Schemaに変換することで、docker compose upの前にエラーを検出する検証が可能になります。

Docker Compose JSONの例

{
  "version": "3.8",
  "services": {
    "web": {
      "image": "nginx:alpine",
      "ports": ["80:80", "443:443"],
      "volumes": ["./nginx.conf:/etc/nginx/nginx.conf:ro"],
      "dependsOn": ["api"],
      "restart": "unless-stopped",
      "healthcheck": {
        "test": ["CMD", "curl", "-f", "http://localhost"],
        "interval": "30s",
        "timeout": "10s",
        "retries": 3
      }
    },
    "api": {
      "build": {
        "context": ".",
        "dockerfile": "Dockerfile"
      },
      "environment": {
        "NODE_ENV": "production",
        "DATABASE_URL": "postgres://db:5432/app"
      },
      "dependsOn": ["db"]
    },
    "db": {
      "image": "postgres:16",
      "volumes": ["pgdata:/var/lib/postgresql/data"],
      "environment": {
        "POSTGRES_DB": "app",
        "POSTGRES_USER": "admin"
      }
    }
  },
  "volumes": {
    "pgdata": {}
  }
}

生成されるYAML Schema

type: object
properties:
  version:
    type: string
    pattern: "^\\d+\\.\\d+$"
  services:
    type: object
    patternProperties:
      "^[a-z][a-z0-9_-]*$":
        type: object
        properties:
          image:
            type: string
          build:
            oneOf:
              - type: string
              - type: object
                properties:
                  context:
                    type: string
                  dockerfile:
                    type: string
                required:
                  - context
          ports:
            type: array
            items:
              type: string
              pattern: "^\\d+:\\d+(/tcp|/udp)?$"
          volumes:
            type: array
            items:
              type: string
          environment:
            oneOf:
              - type: object
                patternProperties:
                  "^[A-Z][A-Z0-9_]*$":
                    type: string
              - type: array
                items:
                  type: string
                  pattern: "^[A-Z][A-Z0-9_]*=.*$"
          dependsOn:
            type: array
            items:
              type: string
          restart:
            type: string
            enum:
              - "no"
              - always
              - on-failure
              - unless-stopped
          healthcheck:
            type: object
            properties:
              test:
                oneOf:
                  - type: string
                  - type: array
                    items:
                      type: string
              interval:
                type: string
                pattern: "^\\d+[smh]$"
              timeout:
                type: string
                pattern: "^\\d+[smh]$"
              retries:
                type: integer
                minimum: 0
        oneOf:
          - required: [image]
          - required: [build]
  volumes:
    type: object
    patternProperties:
      "^[a-z][a-z0-9_-]*$":
        type: [object, "null"]
  networks:
    type: object
    patternProperties:
      "^[a-z][a-z0-9_-]*$":
        type: [object, "null"]
required:
  - services

主要な検証パターン

サービスにはimageまたはbuildが必要:

oneOf:
  - required: [image]
  - required: [build]

ポートマッピングのフォーマット:

pattern: "^\\d+:\\d+(/tcp|/udp)?$"

環境変数の命名:

patternProperties:
  "^[A-Z][A-Z0-9_]*$":
    type: string

カスタム規約への拡張

チームは組織固有のルールを追加できます:

# 本番サービスにはヘルスチェックを必須にする
healthcheck:
  type: object
  required:
    - test
    - interval

IDE統合

YAMLスキーマがあれば、VS CodeのYAML拡張機能がcomposeファイルに対して自動補完、インライン検証、ホバードキュメントを提供します。composeファイルの先頭にスキーマ参照を追加するか、.vscode/settings.jsonで設定してください。

ユースケース

複雑なマルチコンテナセットアップを持つ開発チームには、環境間で一貫したDocker Composeファイルが必要です。YAMLスキーマは、すべてのcomposeファイルが適切なポートマッピング、必要なヘルスチェック、有効なリスタートポリシーを持つことを検証し、開発者マシンとCI環境間の設定のドリフトを検出します。

試してみる — JSON to YAML Schema

フルツールを開く