jpskill.com
🛠️ 開発・MCP コミュニティ

docker-compose

複数のコンテナで構成される開発環境や本番環境を効率的に構築・管理する際に、Docker Compose を活用するSkill。

📜 元の英語説明(参考)

Docker Compose for multi-container local development and production stacks. Use when user mentions "docker-compose", "docker compose", "multi-container", "compose file", "services", "docker networking", "compose volumes", "dev environment setup", or orchestrating multiple containers locally.

🇯🇵 日本人クリエイター向け解説

一言でいうと

複数のコンテナで構成される開発環境や本番環境を効率的に構築・管理する際に、Docker Compose を活用するSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-17
取得日時
2026-05-17
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

Docker Compose

Compose ファイルの構文

docker-compose.yml (または Compose V2 の compose.yml) は、サービス、ネットワーク、およびボリュームを定義します。

# compose.yml
services:
  web:
    image: nginx:1.25-alpine
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    networks:
      - frontend
    depends_on:
      api:
        condition: service_healthy

  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://app:secret@db:5432/myapp
      REDIS_URL: redis://cache:6379
    networks:
      - frontend
      - backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 15s

  db:
    image: postgres:16-alpine
    volumes:
      - pgdata:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: myapp
    networks:
      - backend

  cache:
    image: redis:7-alpine
    command: redis-server --maxmemory 128mb --maxmemory-policy allkeys-lru
    networks:
      - backend

volumes:
  pgdata:

networks:
  frontend:
  backend:

一般的なサービスパターン

Web + API + データベース + キャッシュ

上記の例に示されています。Web サーバーは API にリバースプロキシし、API は共有バックエンドネットワーク上でデータベースとキャッシュの両方と通信します。

フロントエンド + バックエンド + データベース

services:
  frontend:
    build: ./frontend
    ports:
      - "5173:5173"
    volumes:
      - ./frontend/src:/app/src
    environment:
      VITE_API_URL: http://localhost:4000

  backend:
    build: ./backend
    ports:
      - "4000:4000"
    volumes:
      - ./backend/src:/app/src
    depends_on:
      db:
        condition: service_healthy
    environment:
      DB_HOST: db

  db:
    image: mysql:8
    volumes:
      - mysql_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: app
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 5s
      retries: 5

volumes:
  mysql_data:

サービス間のネットワーク

同じネットワーク上のサービスは、サービス名で互いに到達できます。DNS 解決は自動です。

networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge
    internal: true  # no external access
  • frontend および backend ネットワーク上のサービスは、両方のサービスと通信できます。
  • backend のみにあるサービスは、internal: true の場合、インターネットに到達できません。
  • サービス名をホスト名として使用します: postgres://db:5432http://api:3000

実行時にネットワークの問題を検査するには:

docker network ls
docker network inspect myproject_backend
docker compose exec api ping db

ボリューム管理

名前付きボリューム

コンテナの再起動後もデータを永続化します。Docker がストレージの場所を管理します。

volumes:
  pgdata:
    driver: local

バインドマウント

ホストディレクトリをコンテナにマッピングします。開発時のホットリロードに不可欠です。

services:
  api:
    volumes:
      - ./src:/app/src          # source code
      - /app/node_modules       # anonymous volume to prevent overwrite

tmpfs

インメモリストレージです。永続化すべきではないシークレットやスクラッチデータに役立ちます。

services:
  api:
    tmpfs:
      - /tmp
      - /run:size=64M

読み取り専用マウント

volumes:
  - ./config:/etc/app/config:ro

環境変数

インライン

services:
  api:
    environment:
      NODE_ENV: production
      LOG_LEVEL: info

.env ファイルから

services:
  api:
    env_file:
      - .env
      - .env.local

変数置換

Compose は、YAML 自体での変数置換のために、プロジェクトルートにある .env ファイルを自動的に読み取ります。

# .env
POSTGRES_VERSION=16
APP_PORT=3000
services:
  db:
    image: postgres:${POSTGRES_VERSION}-alpine
  api:
    ports:
      - "${APP_PORT}:3000"

デフォルト値には ${VAR:-default} を、必須変数には ${VAR:?error message} を使用します。

ヘルスチェックと depends_on

depends_on だけではコンテナが起動するのを待つだけで、内部のサービスが準備完了になるのを待ちません。条件を使用してください:

services:
  api:
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started
      migrations:
        condition: service_completed_successfully

利用可能な条件:

  • service_started -- コンテナが起動しました (デフォルト)
  • service_healthy -- ヘルスチェックがパスしています
  • service_completed_successfully -- コンテナがコード 0 で終了しました

Postgres のヘルスチェックの例:

healthcheck:
  test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
  interval: 5s
  timeout: 3s
  retries: 5
  start_period: 10s

開発用オーバーライド

Compose は、ベースファイルの上に docker-compose.override.yml (または compose.override.yml) を自動的にマージします。これは開発固有の設定に使用します。

# compose.yml (base)
services:
  api:
    build: ./api
    environment:
      NODE_ENV: production

# compose.override.yml (dev, loaded automatically)
services:
  api:
    build:
      target: development
    volumes:
      - ./api/src:/app/src
    environment:
      NODE_ENV: development
      DEBUG: "app:*"
    ports:
      - "9229:9229"  # debugger port

明示的なファイル選択の場合:

docker compose -f compose.yml -f compose.prod.yml up -d

便利なコマンド

# すべてのサービスを開始 (デタッチモード)
docker compose up -d

# 開始して強制的にリビルド
docker compose up -d --build

# コンテナ、ネットワークを停止して削除
docker compose down

# 停止してボリュームも削除 (データを破棄)
docker compose down -v

# ログを表示 (フォローモード、最後の 100 行)
docker compose logs -f --tail=100
docker compose logs -f api db    # 特定のサービス

# 一度限りのコマンドを実行
docker compose exec api sh
docker compose 
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Docker Compose

Compose File Syntax

A docker-compose.yml (or compose.yml in Compose V2) defines services, networks, and volumes.

# compose.yml
services:
  web:
    image: nginx:1.25-alpine
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    networks:
      - frontend
    depends_on:
      api:
        condition: service_healthy

  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgres://app:secret@db:5432/myapp
      REDIS_URL: redis://cache:6379
    networks:
      - frontend
      - backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 15s

  db:
    image: postgres:16-alpine
    volumes:
      - pgdata:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: myapp
    networks:
      - backend

  cache:
    image: redis:7-alpine
    command: redis-server --maxmemory 128mb --maxmemory-policy allkeys-lru
    networks:
      - backend

volumes:
  pgdata:

networks:
  frontend:
  backend:

Common Service Patterns

Web + API + Database + Cache

Shown in the example above. The web server reverse-proxies to the API, which talks to both the database and cache on a shared backend network.

Frontend + Backend + Database

services:
  frontend:
    build: ./frontend
    ports:
      - "5173:5173"
    volumes:
      - ./frontend/src:/app/src
    environment:
      VITE_API_URL: http://localhost:4000

  backend:
    build: ./backend
    ports:
      - "4000:4000"
    volumes:
      - ./backend/src:/app/src
    depends_on:
      db:
        condition: service_healthy
    environment:
      DB_HOST: db

  db:
    image: mysql:8
    volumes:
      - mysql_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: app
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 5s
      retries: 5

volumes:
  mysql_data:

Networking Between Services

Services on the same network can reach each other by service name. DNS resolution is automatic.

networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge
    internal: true  # no external access
  • A service on frontend and backend networks can talk to services on both.
  • A service only on backend cannot reach the internet if internal: true.
  • Use service names as hostnames: postgres://db:5432, http://api:3000.

To inspect networking issues at runtime:

docker network ls
docker network inspect myproject_backend
docker compose exec api ping db

Volume Management

Named Volumes

Persist data across container restarts. Docker manages the storage location.

volumes:
  pgdata:
    driver: local

Bind Mounts

Map host directories into containers. Essential for development hot-reload.

services:
  api:
    volumes:
      - ./src:/app/src          # source code
      - /app/node_modules       # anonymous volume to prevent overwrite

tmpfs

In-memory storage. Useful for secrets or scratch data that should not persist.

services:
  api:
    tmpfs:
      - /tmp
      - /run:size=64M

Read-Only Mounts

volumes:
  - ./config:/etc/app/config:ro

Environment Variables

Inline

services:
  api:
    environment:
      NODE_ENV: production
      LOG_LEVEL: info

From .env File

services:
  api:
    env_file:
      - .env
      - .env.local

Variable Substitution

Compose reads a .env file in the project root automatically for variable substitution in the YAML itself.

# .env
POSTGRES_VERSION=16
APP_PORT=3000
services:
  db:
    image: postgres:${POSTGRES_VERSION}-alpine
  api:
    ports:
      - "${APP_PORT}:3000"

Use defaults with ${VAR:-default} and required variables with ${VAR:?error message}.

Health Checks and depends_on

depends_on alone only waits for the container to start, not for the service inside to be ready. Use conditions:

services:
  api:
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started
      migrations:
        condition: service_completed_successfully

Available conditions:

  • service_started -- container has started (default)
  • service_healthy -- healthcheck is passing
  • service_completed_successfully -- container exited with code 0

A healthcheck example for Postgres:

healthcheck:
  test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
  interval: 5s
  timeout: 3s
  retries: 5
  start_period: 10s

Development Overrides

Compose automatically merges docker-compose.override.yml (or compose.override.yml) on top of the base file. Use this for development-specific settings.

# compose.yml (base)
services:
  api:
    build: ./api
    environment:
      NODE_ENV: production

# compose.override.yml (dev, loaded automatically)
services:
  api:
    build:
      target: development
    volumes:
      - ./api/src:/app/src
    environment:
      NODE_ENV: development
      DEBUG: "app:*"
    ports:
      - "9229:9229"  # debugger port

For explicit file selection:

docker compose -f compose.yml -f compose.prod.yml up -d

Useful Commands

# Start all services (detached)
docker compose up -d

# Start and force rebuild
docker compose up -d --build

# Stop and remove containers, networks
docker compose down

# Stop and also remove volumes (destroys data)
docker compose down -v

# View logs (follow mode, last 100 lines)
docker compose logs -f --tail=100
docker compose logs -f api db    # specific services

# Run a one-off command
docker compose exec api sh
docker compose exec db psql -U app -d myapp

# Run a new container (not exec into existing)
docker compose run --rm api npm test

# List running services
docker compose ps

# Pull latest images
docker compose pull

# Restart a single service
docker compose restart api

# Scale a service (stateless services only)
docker compose up -d --scale worker=3

# View resource usage
docker compose top
docker stats

Production Considerations

services:
  api:
    restart: unless-stopped          # or: always, on-failure, no
    read_only: true
    security_opt:
      - no-new-privileges:true
    tmpfs:
      - /tmp
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 512M
        reservations:
          cpus: "0.25"
          memory: 128M
    secrets:
      - db_password

secrets:
  db_password:
    file: ./secrets/db_password.txt   # available at /run/secrets/db_password

Debugging

# Container won't start -- check logs and exit codes
docker compose ps -a
docker compose logs api
docker compose run --rm api sh          # interactive shell to diagnose

# Service can't reach another service
docker compose exec api getent hosts db
docker network inspect myproject_backend

# Inspect environment and mounts inside a container
docker compose exec api env | sort
docker inspect $(docker compose ps -q db) --format '{{json .Mounts}}' | jq

# Resource usage
docker stats --no-stream

# Validate compose file without starting
docker compose config --quiet