# Define the version of the Docker Compose file format being used.
version: '3.9'

# Define services that will run in containers
services:

  # Configuration for MySQL Service
  adatlas_db:
    # Assign a specific name to the container
    container_name: adatlas_db
    # Use the latest MySQL image from Docker Hub
    image: mysql:latest 
    # Specify command-line options for MySQL container
    command: --authentication_policy=caching_sha2_password
    # Always restart the container if it stops
    restart: always
    # Load environment variables from the specified file
    env_file:
      - ../src/.env  
    # Map a volume for persistent MySQL data
    volumes:
      - db_data:/var/lib/mysql   
    # Set environment variables for MySQL container
    environment:
      MYSQL_INIT_CONNECT: "SET GLOBAL host_cache_size=0;" 
      MYSQL_DEFAULT_AUTHENTICATION_PLUGIN: caching_sha2_password
      MYSQL_ROOT_HOST: '%'
      MYSQL_ROOT_PASSWORD: 9Tn8<+A!OeB3
    # Add health check configuration for MySQL container
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]  # Simple health check to ping the MySQL server
      interval: 30s  # Health check will run every 30 seconds
      timeout: 10s   # If health check command doesn't return within 10 seconds, it's considered a failure
      retries: 3     # Container marked as unhealthy after 3 consecutive failures
    # Attach the service to a custom network
    networks:
      - adatlas_network
  
  # # Configuration for Database Viewer using Adminer
  # db_viewer:
  #   # Assign a specific name to the container
  #   container_name: db_viewer
  #   # Use the latest Adminer image from Docker Hub
  #   image: adminer:latest  
  #   # Map port 8080 from host to container
  #   ports:
  #     - 8080:8080 
  #   # Specify dependency on the MySQL service
  #   depends_on: 
  #     adatlas_db:
  #       condition: service_healthy 
  #   # Attach the service to a custom network
  #   networks:
  #     - adatlas_network
 
  # Configuration for Django Web Service
  adatlas_web:
    # Assign a specific name to the container
    container_name: adatlas_web
    # Use the custom-built Docker image for the web service
    image: adatlas_web:latest
    # Build context for Docker build, specifying Dockerfile location and target
    build:
      context: ../
      dockerfile: docker/Dockerfile
      target: development 
    # Load environment variables from the specified file
    env_file:
      - ../src/.env
    # Map port 8000 from host to container
    ports:
      - "8024:8000"
    # # Mount the source code directory from the host to the container
    volumes:
      - ../src:/app
    # Specify dependency on the MySQL service 
    depends_on:  
      adatlas_db:
        condition: service_healthy  
    # Attach the service to a custom network
    networks:
      - adatlas_network
 
# Define a custom network for services to communicate with each other
networks:
  adatlas_network:
    # Use the bridge driver for the network
    driver: bridge              

# Define a volume for persisting MySQL data
volumes:
  db_data:
