bitrixdock icon indicating copy to clipboard operation
bitrixdock copied to clipboard

healthcheck dependencies

Open phoedos opened this issue 1 year ago • 1 comments

please add healthcheck dependencies adminer -> db(healthy) php -> db(healthy)

---
services:
    php:
        build: ./php/${PHP_VERSION}
        volumes_from:
            - source
        links:
            - db
            - memcached
        environment:
            TZ: Europe/Moscow
        stdin_open: true
        tty: true
        networks:
            - bitrixdock
        restart: unless-stopped
        healthcheck:
            test: ["CMD-SHELL", "php --ini || exit 1"]
            interval: 30s
            timeout: 10s
            retries: 10
            start_period: 10s  
        depends_on:
            db: 
             condition: service_healthy    

    web_server:
        build: ./${WEB_SERVER_TYPE}
        depends_on:
            - source
            - memcached
            - php
            - db
        volumes_from:
            - source
        ports:
            - '${INTERFACE}:80:80'
            - '${INTERFACE}:443:443'
        links:
            - php
        networks:
            - bitrixdock
        environment:
            TZ: Europe/Moscow
        stdin_open: true
        tty: true
        restart: unless-stopped
        healthcheck:
            test: ["CMD-SHELL", "curl -f http://localhost || exit 1"]
            interval: 30s
            timeout: 10s
            retries: 10
            start_period: 10s

    db:
        build: ./${DB_SERVER_TYPE}
        volumes:
            - ./${DB_SERVER_TYPE}/init:/docker-entrypoint-initdb.d
        volumes_from:
            - source
        ports:
            - '${INTERFACE}:3306:3306'
        environment:
            MYSQL_DATABASE: ${MYSQL_DATABASE}
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
            TZ: Europe/Moscow
        command: mysqld --user=root --sql-mode=""
        networks:
            - bitrixdock
        stdin_open: true
        tty: true
        restart: unless-stopped
        healthcheck:
            test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD"]
            interval: 10s
            timeout: 5s
            retries: 10
      

    memcached:
        image: memcached:latest
        volumes_from:
            - source
        networks:
            - bitrixdock
        environment:
            TZ: Europe/Moscow
        stdin_open: true
        tty: true
        restart: unless-stopped

    adminer:
        image: dockette/adminer:full
        links:
            - db:db
        ports:
            - '${INTERFACE}:8080:80'
        environment:
            UPLOAD: 1024M # upload_max_filesize, post_max_size
            TZ: Europe/Moscow
        restart: unless-stopped
        stdin_open: true
        tty: true
        networks:
            - bitrixdock
        depends_on:
            db: 
             condition: service_healthy

    source:
        image: alpine:latest
        volumes:
            - ./logs/${WEB_SERVER_TYPE}:/var/log/${WEB_SERVER_TYPE}
            - ./logs/php:/var/log/php
            - ./logs/db:/var/log/mysql
            - ./logs/memcached:/var/log/memcached
            - db:/var/lib/mysql
            - cache:/var/lib/memcached
            - ${SITE_PATH}:/var/www/bitrix
            - /etc/localtime:/etc/localtime/:ro
        networks:
            - bitrixdock

volumes:
    db:
        driver: local
    cache:
        driver: local

networks:
    bitrixdock:

phoedos avatar Dec 11 '24 11:12 phoedos

Impact Analysis

In scenarios where the database takes time to start (e.g., 20 seconds to become healthy), implementing this healthcheck dependency will provide the following benefits:

Before the fix:

  • All services (PHP, Adminer, database) start simultaneously
  • PHP and Adminer may fail or throw errors for ~20 seconds while trying to connect to an unavailable database
  • Connection errors appear in logs until the database becomes ready

After implementing this change:

  • Database starts first and takes its normal time to become healthy
  • Only after the database passes its healthcheck (MySQL ping) will PHP and Adminer services start
  • No connection errors or failed attempts from dependent services
  • Total startup time remains the same (~20 seconds), but with cleaner, more reliable startup sequence

This ensures proper service orchestration by making PHP and Adminer wait for a confirmed healthy database before attempting to connect, eliminating startup race conditions.

paskal avatar Jun 15 '25 14:06 paskal