services: # PostgreSQL Database db: image: postgres:16-alpine container_name: iot_db environment: POSTGRES_DB: iot POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres volumes: - db_data:/var/lib/postgresql/data - ./backend/scripts/init-db.sh:/docker-entrypoint-initdb.d/init-db.sh:ro ports: - "5432:5432" healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres -d iot"] interval: 30s timeout: 10s retries: 5 restart: unless-stopped networks: - iot_net # Redis Cache & Session Store redis: image: redis:7-alpine container_name: iot_redis command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru volumes: - redis_data:/data ports: - "6379:6379" healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 30s timeout: 10s retries: 5 restart: unless-stopped networks: - iot_net # MQTT Broker mqtt: image: eclipse-mosquitto:2 container_name: iot_mqtt volumes: - ./docker/mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf:ro - mqtt_data:/mosquitto/data - mqtt_logs:/mosquitto/log ports: - "1883:1883" # MQTT - "9001:9001" # WebSocket healthcheck: test: ["CMD", "mosquitto_pub", "-h", "localhost", "-t", "test", "-m", "test"] interval: 30s timeout: 10s retries: 5 restart: unless-stopped networks: - iot_net # Django Backend API api: build: context: ./backend dockerfile: Dockerfile container_name: iot_api environment: DJANGO_SETTINGS_MODULE: core.settings DATABASE_URL: postgres://postgres:postgres@db:5432/iot REDIS_URL: redis://redis:6379/0 MQTT_BROKER_URL: mqtt MQTT_BROKER_PORT: 1883 ALLOWED_HOSTS: "*" DJANGO_DEBUG: 1 CORS_ALLOWED_ORIGINS: http://localhost:5173,http://localhost:3000,http://web:5173,http://0.0.0.0:5173 DJANGO_SUPERUSER_EMAIL: admin@example.com DJANGO_SUPERUSER_PASSWORD: admin123 CELERY_BROKER_URL: redis://redis:6379/1 CELERY_RESULT_BACKEND: redis://redis:6379/1 volumes: - ./backend:/app - static_volume:/app/staticfiles - media_volume:/app/media ports: - "8000:8000" depends_on: db: condition: service_healthy redis: condition: service_healthy mqtt: condition: service_healthy restart: unless-stopped networks: - iot_net # Celery Worker for Background Tasks celery: build: context: ./backend dockerfile: Dockerfile container_name: iot_celery environment: DJANGO_SETTINGS_MODULE: core.settings DATABASE_URL: postgres://postgres:postgres@db:5432/iot REDIS_URL: redis://redis:6379/0 MQTT_BROKER_URL: mqtt MQTT_BROKER_PORT: 1883 CELERY_BROKER_URL: redis://redis:6379/1 CELERY_RESULT_BACKEND: redis://redis:6379/1 volumes: - ./backend:/app command: sh -c "python -m celery -A core worker -l info" depends_on: db: condition: service_healthy redis: condition: service_healthy api: condition: service_started healthcheck: test: ["CMD", "python", "-m", "celery", "-A", "core", "inspect", "ping"] interval: 30s timeout: 10s retries: 3 start_period: 10s restart: unless-stopped networks: - iot_net # React Frontend (Development) web: build: context: ./frontend dockerfile: Dockerfile target: development container_name: iot_web environment: VITE_API_BASE: http://localhost:8000 VITE_WS_BASE: ws://localhost:8000 VITE_MQTT_WS_URL: ws://localhost:9001 volumes: - ./frontend:/usr/src/app - /usr/src/app/node_modules ports: - "5173:5173" depends_on: - api restart: unless-stopped networks: - iot_net # Nginx Reverse Proxy (Production) nginx: image: nginx:alpine container_name: iot_nginx volumes: - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro - ./docker/nginx/sites-available:/etc/nginx/sites-available:ro - static_volume:/app/staticfiles:ro - media_volume:/app/media:ro ports: - "80:80" - "443:443" depends_on: - api - web restart: unless-stopped networks: - iot_net profiles: - production volumes: db_data: redis_data: mqtt_data: mqtt_logs: static_volume: media_volume: networks: iot_net: driver: bridge