Docker Rails環境のネットワーク構成図 - nyg1971/business_api GitHub Wiki

Docker + Rails環境のネットワーク構成図

🏗️ 全体構成

┌─────────────────────────────────────────────────────────────────┐
│                        macOS ホスト環境                           │
│                                                                 │
│  🌐 ブラウザ              💻 ターミナル           🔧 pgAdmin     │
│  localhost:3000          curl localhost:3000     localhost:8080  │
│        │                        │                     │         │
│        └────────────────────────┼─────────────────────┘         │
│                                 │                               │
└─────────────────────────────────┼───────────────────────────────┘
                                  │
                    📡 ポートマッピング (3000:3000)
                                  │
┌─────────────────────────────────┼───────────────────────────────┐
│                    Docker Desktop                                │
│                                 │                               │
│  ┌─────────────────────────────────────────────────────────────┐ │
│  │            business_api_default ネットワーク                  │ │
│  │                                                             │ │
│  │  ┌─────────────────┐  ┌─────────────────┐  ┌──────────────┐ │ │
│  │  │ business_api_app │  │business_api_    │  │business_api_ │ │ │
│  │  │                 │  │postgres         │  │redis         │ │ │
│  │  │   🚂 Rails      │  │                 │  │              │ │ │
│  │  │   Puma Server   │  │  🐘 PostgreSQL  │  │  🔴 Redis    │ │ │
│  │  │   Port: 3000    │  │  Port: 5432     │  │  Port: 6379  │ │ │
│  │  │                 │  │  DB: business_  │  │  Cache &     │ │ │
│  │  │                 │  │  api_development│  │  Session     │ │ │
│  │  └─────────────────┘  └─────────────────┘  └──────────────┘ │ │
│  │           │                     │                    │      │ │
│  │           └─────────────────────┼────────────────────┘      │ │
│  │                                 │                           │ │
│  └─────────────────────────────────────────────────────────────┘ │
│                                                                   │
│  ┌─────────────────────────────────────────────────────────────┐ │
│  │                   Docker Volume                              │ │
│  │                                                             │ │
│  │           💾 postgres_data                                  │ │
│  │           (データベースファイル永続化)                         │ │
│  │                                                             │ │
│  └─────────────────────────────────────────────────────────────┘ │
└───────────────────────────────────────────────────────────────────┘

🔗 通信の流れ

1. 外部からのアクセス(ホスト → Docker)

ブラウザ/curl (localhost:3000) 
    ↓ 【HTTPリクエスト】
Docker ポートマッピング (3000:3000)
    ↓
business_api_app コンテナ (Rails)

2. Rails内部からの通信(Docker内部)

Rails アプリケーション
    ↓ 【データベースクエリ】
    host: "postgres" (サービス名で解決)
    port: 5432
    ↓
business_api_postgres コンテナ (PostgreSQL)

Rails アプリケーション  
    ↓ 【キャッシュ・セッション】
    host: "redis" (サービス名で解決)
    port: 6379
    ↓
business_api_redis コンテナ (Redis)

⚙️ 設定ファイルでの指定

database.yml

development:
  adapter: postgresql
  host: postgres      # ← コンテナ名(サービス名)で指定
  port: 5432         # ← コンテナ内部ポート
  database: business_api_development
  username: business_user
  password: business_password

docker-compose.yml

services:
  postgres:           # ← この名前がhost名になる
    image: postgres:15
    ports:
      - "5432:5432"   # ← ホスト:コンテナ のポートマッピング
    
  redis:              # ← この名前がhost名になる  
    image: redis:alpine
    ports:
      - "6379:6379"
      
  app:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - postgres      # ← 依存関係の指定
      - redis

🎯 確認した通信パターン

ヘルスチェック時の流れ

1. curl http://localhost:3000/up
   ↓
2. Docker ポートマッピング (3000:3000)
   ↓  
3. Rails (business_api_app)
   ↓
4. Rails → PostgreSQL (host: postgres, port: 5432)
   ↓
5. ActiveRecord::SchemaMigration Load (データベース接続確認)
   ↓
6. Rails → HTTP 200 OK レスポンス
   ↓
7. curl で緑色の画面表示

🔑 重要なポイント

Docker内部通信

  • サービス名で通信: postgres, redis がホスト名
  • 内部ポート使用: 5432, 6379 (ポートマッピングは外部アクセス用)
  • 自動的なDNS解決: Docker Composeが自動でサービス名を解決

外部アクセス

  • ポートマッピング必須: ports: ["3000:3000"]
  • localhost経由: ホスト環境からはlocalhost:3000でアクセス

データ永続化

  • Docker Volume: postgres_dataでデータベースファイルを永続化
  • コンテナ削除でもデータ保持: 安全な開発環境

この構成により、開発環境の一貫性本番環境への移行しやすさを両立!