プロジェクト

全般

プロフィール

バグ #228

未完了

[Task5] Docker設定統合と環境変数最適化

Redmine Admin さんが3日前に追加.

ステータス:
新規
優先度:
通常
担当者:
-
開始日:
2025-06-04
期日:
進捗率:

0%

予定工数:

説明

Task 5: Docker設定統合と環境変数最適化

概要

task2-service のDocker設定を最適化し、ニュース機能統合に必要な環境変数・設定を追加する

作業ディレクトリ

/home/ito/task2-service/

実装ファイル

├── docker-compose.yml          # メイン統合設定
├── docker-compose-prod.yml     # 本番環境設定
├── .env                        # 環境変数テンプレート
├── .env.example               # 環境変数例
├── nginx-prod.conf            # Nginx設定最適化
└── healthcheck.sh             # ヘルスチェックスクリプト

実装仕様

1. Docker Compose設定統合

# docker-compose.yml
version: '3.8'

services:
  # Task2 API Server (統合)
  task2-api:
    image: node:18-alpine
    container_name: task2-api
    restart: unless-stopped
    working_dir: /app
    ports:
      - "3002:3002"
    environment:
      - NODE_ENV=${NODE_ENV:-development}
      - PORT=3002
      # Redmine設定
      - REDMINE_URL=https://call2arm.com
      - REDMINE_API_KEY=${REDMINE_API_KEY}
      # News API設定
      - NEWS_API_KEY=${NEWS_API_KEY}
      - NEWS_API_URL=https://newsapi.org/v2
      # Database設定 (SQLite/PostgreSQL切替)
      - DB_TYPE=${DB_TYPE:-sqlite}
      - DB_PATH=/app/data/news.db
      - POSTGRES_URL=${POSTGRES_URL}
      # CORS設定
      - ALLOWED_ORIGINS=*.call2arm.com,localhost:3000
    volumes:
      - ./api:/app
      - task2_data:/app/data
    command: sh -c "npm install && npm start"
    networks:
      - task2-network
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://127.0.0.1:3002/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Task2 UI Development Server
  task2-ui-dev:
    image: node:18-alpine
    container_name: task2-ui-dev
    restart: unless-stopped
    working_dir: /app
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=development
      - VITE_API_URL=http://task2-api:3002
      - VITE_APP_TITLE=Task2 Redmine Integration
      - VITE_REDMINE_URL=https://call2arm.com
    volumes:
      - ./ui:/app
      - /app/node_modules
    command: sh -c "npm install && npm run dev -- --host 0.0.0.0"
    depends_on:
      task2-api:
        condition: service_healthy
    networks:
      - task2-network

  # Nginx Production Ready
  task2-nginx:
    image: nginx:alpine
    container_name: task2-nginx
    restart: unless-stopped
    ports:
      - "3008:80"
    volumes:
      - ./nginx-prod.conf:/etc/nginx/nginx.conf:ro
      - ./ui/dist:/usr/share/nginx/html:ro
      - task2_logs:/var/log/nginx
    depends_on:
      - task2-api
    networks:
      - task2-network
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://127.0.0.1/health"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  task2_data:
    driver: local
  task2_logs:
    driver: local

networks:
  task2-network:
    driver: bridge

2. 環境変数設定

# .env.example
# =================================
# Task2 Service Configuration
# =================================

# Environment
NODE_ENV=development

# API Keys
REDMINE_API_KEY=feb66d81a5f4ff9c585ce30fce2ac06e0554aec6
NEWS_API_KEY=your_news_api_key_here

# Database
DB_TYPE=sqlite
DB_PATH=/app/data/news.db
# POSTGRES_URL=postgresql://user:pass@host:5432/dbname

# External Services
REDMINE_URL=https://call2arm.com
NEWS_API_URL=https://newsapi.org/v2

# CORS
ALLOWED_ORIGINS=*.call2arm.com,localhost:3000,localhost:3008

# Logging
LOG_LEVEL=info

3. Nginx設定最適化

# nginx-prod.conf
events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # Logging
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                   '$status $body_bytes_sent "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log warn;

    # Performance
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;

    server {
        listen 80;
        server_name localhost;
        root /usr/share/nginx/html;
        index index.html;

        # Health check
        location /health {
            access_log off;
            return 200 "healthy\n";
            add_header Content-Type text/plain;
        }

        # API Proxy
        location /api/ {
            proxy_pass http://task2-api:3002/api/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_cache_bypass $http_upgrade;
            
            # CORS Headers
            add_header Access-Control-Allow-Origin "*" always;
            add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
            add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Redmine-API-Key" always;
        }

        # SPA Routing
        location / {
            try_files $uri $uri/ /index.html;
        }

        # Static assets caching
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
            expires 1y;
            add_header Cache-Control "public, immutable";
        }
    }
}

4. ヘルスチェックスクリプト

#!/bin/bash
# healthcheck.sh

API_URL="http://localhost:3002/api/health"
UI_URL="http://localhost:3000"

# API Health Check
echo "Checking API health..."
if curl -f -s "$API_URL" > /dev/null; then
    echo "✅ API is healthy"
else
    echo "❌ API is unhealthy"
    exit 1
fi

# UI Health Check
echo "Checking UI health..."
if curl -f -s "$UI_URL" > /dev/null; then
    echo "✅ UI is healthy"
else
    echo "❌ UI is unhealthy"
    exit 1
fi

echo "🎉 All services are healthy"

成果物

  • docker-compose.yml統合更新
  • .env.example作成
  • nginx-prod.conf最適化
  • healthcheck.sh作成
  • docker-compose-prod.yml作成
  • README.md更新

設定要件

環境分離

  • development: フル開発環境
  • production: 最適化ビルド
  • testing: テスト専用設定

セキュリティ

  • 環境変数による秘密情報管理
  • CORS適切な設定
  • ログ設定最適化

パフォーマンス

  • Nginx gzip圧縮
  • 静的ファイルキャッシュ
  • ヘルスチェック最適化

デプロイメント手順

# 開発環境起動
docker-compose up -d

# 本番環境起動
docker-compose -f docker-compose-prod.yml up -d

# ヘルスチェック
./healthcheck.sh

# ログ確認
docker-compose logs -f task2-api

テスト項目

  • Docker Compose起動確認
  • 環境変数設定確認
  • ヘルスチェック動作確認
  • CORS設定確認
  • Nginx プロキシ動作確認
  • ログ出力確認

完了条件

  • Docker設定統合完了
  • 環境変数最適化完了
  • ヘルスチェック実装完了
  • Nginx設定最適化完了
  • 全サービス正常起動確認

参照: 親チケット #223
実装目標: 1日
優先度: 中

表示するデータがありません

他の形式にエクスポート: Atom PDF