name: dsql

services:
    setup:
        build:
            context: ./docker/setup
            dockerfile: Dockerfile
        env_file: .env
        container_name: dsql-setup
        hostname: dsql-setup
        volumes:
            - .:/app
            - ${DSQL_VOLUME_DB_SSL:-../dsql-data/ssl}:/ssl
            - ${DSQL_VOLUME_SSH:-../dsql-data/ssh}:/ssh
        environment:
            - DSQL_SSL_DIR=/app/ssl
            - DSQL_DB_SCHEMA_DIR=/app/jsonData/dbSchemas
            - DSQL_APP_DIR=/app

    cron:
        build:
            context: ./docker/cron
            dockerfile: Dockerfile
        env_file: .env
        networks:
            datasquirel:
                ipv4_address: 172.72.0.27
        container_name: dsql-cron
        hostname: dsql-cron
        volumes:
            - ${DSQL_VOLUME_STATIC:-../dsql-data/static}:/static
            - ${DSQL_VOLUME_APP:-.}:/app
        environment:
            - DSQL_SSL_DIR=/app/ssl
            - DSQL_DB_SCHEMA_DIR=/app/jsonData/dbSchemas
            - DSQL_APP_DIR=/app
        restart: on-failure:${DSQL_WEB_APP_FAIL_COUNTS:-3}
        depends_on:
            setup:
                condition: service_completed_successfully
            db:
                condition: service_started

    reverse-proxy:
        image: nginx:alpine
        env_file: .env
        depends_on:
            setup:
                condition: service_completed_successfully
        networks:
            datasquirel:
                ipv4_address: 172.72.0.34
        container_name: dsql-reverse-proxy
        ports:
            - ${DSQL_PORT:-7070}:80
        restart: always
        volumes:
            - ./docker/reverse-proxy/conf.d:/etc/nginx/conf.d
    webapp:
        build:
            context: .
            dockerfile: ${DSQL_DOCKERFILE:-Dockerfile}
        env_file: .env
        networks:
            datasquirel:
                ipv4_address: 172.72.0.35
        container_name: dsql-site
        hostname: dsql-site
        volumes:
            - ${DSQL_VOLUME_STATIC:-../dsql-data/static}:/static
            - ${DSQL_VOLUME_APP:-.}:/app
        environment:
            - DSQL_SSL_DIR=/app/ssl
            - DSQL_DB_SCHEMA_DIR=/app/jsonData/dbSchemas
            - DSQL_APP_DIR=/app
        restart: on-failure:${DSQL_WEB_APP_FAIL_COUNTS:-1}
        depends_on:
            setup:
                condition: service_completed_successfully
            db:
                condition: service_started

    websocket:
        build:
            context: ./docker/web-socket
            dockerfile: Dockerfile
        env_file: .env
        container_name: dsql-websocket
        hostname: dsql-websocket
        volumes:
            - ${DSQL_VOLUME_STATIC:-../dsql-data/static}:/static
            - ${DSQL_VOLUME_APP:-.}:/app
        networks:
            datasquirel:
                ipv4_address: 172.72.0.36
        depends_on:
            reverse-proxy:
                condition: service_started
        restart: always
        environment:
            - DSQL_SSL_DIR=/app/ssl
            - DSQL_DB_SCHEMA_DIR=/app/jsonData/dbSchemas
            - DSQL_APP_DIR=/app

    static:
        image: nginx:alpine
        env_file: .env
        container_name: dsql-static
        volumes:
            - ${DSQL_VOLUME_STATIC:-../dsql-data/static}:/static
            - ./docker/static/conf.d:/etc/nginx/conf.d
        ports:
            - ${DSQL_STATIC_SERVER_PORT}:80
        restart: always
        user: root

    db:
        image: mariadb:11-jammy
        env_file: .env
        networks:
            datasquirel:
                ipv4_address: 172.72.0.32
        depends_on:
            setup:
                condition: service_completed_successfully
        container_name: dsql-site-database
        volumes:
            - ${DSQL_VOLUME_DB:-../dsql-data/db}:/var/lib/mysql
            - ${DSQL_VOLUME_DB_SSL:-../dsql-data/ssl}:/ssl
            - ./docker/mariadb/master/conf.d:/etc/mysql/conf.d
            - ./docker/mariadb/.bash_history:/root/.bash_history
        environment:
            - MARIADB_ROOT_PASSWORD=${DSQL_MARIADB_ROOT_PASSWORD:-password}
        restart: always

    db-load-balancer:
        image: nginx:alpine
        env_file: .env
        networks:
            datasquirel:
                ipv4_address: 172.72.0.24
        depends_on:
            db:
                condition: service_started
        container_name: dsql-db-load-balancer
        hostname: dsql-db-load-balancer
        volumes:
            - ${DSQL_DB_LOAD_BALANCER_CONFIGURATION_FILE:-../dsql-data/db-load-balancer-config/nginx.conf}:/etc/nginx/nginx.conf
        ports:
            - ${DSQL_DB_EXPOSED_PORT:-33600}:3306
        restart: always

networks:
    datasquirel:
        driver: bridge
        ipam:
            config:
                - subnet: 172.72.0.0/24
                  gateway: 172.72.0.1
