# Add healthcheck checks to docker-compose.yml
# Symptoms

How can I add healthcheck checks and dependencies in docker-compose.yml so that the health of the containers is automatically checked by Docker?

# Solution

For systems installed after July 1, 2026, these checks are added automatically at the time of Swarmica installation.

For systems installed earlier, you need to do the following:

1.  Log in to the Swarmica server with `root` permissions.
2.  Go to the Swarmica installation directory, usually `/root/swarmica`:
    ```shell
    cd /root/swarmica
    ```
3.  Create a backup of `.env`:
    ```shell
    cp .env{,.backup}
    ```
4.  Make sure the following parameters are in `.env`. If they are not, then add them:
    ```shell
    DJANGO_ALLOWED_HOSTS=...,django
    INSECURE_HTTP_SERVER=1
    ```
5.  Create a backup of `docker-compose.yml`:
    ```shell
    cp docker-compose.yml{,.backup}
    ```
6.  Edit the `postgres` service, adding `healthcheck`:
    ```yaml
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    ```
    So, the entire `postgres` section will look something like this:
    ```yaml
    postgres:
      image: postgres:14.2-bullseye
      restart: always
      container_name: swarmica-postgres
      shm_size: 1gb
      command: postgres -N 400 -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c track_activity_query_size=16384 -c wal_keep_size=512MB
      healthcheck:
        test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
        interval: 10s
        timeout: 5s
        retries: 5
        start_period: 10s
      volumes:
        - swarmica_postgres_data:/var/lib/postgresql/data:Z
        - swarmica_postgres_data_backups:/backups:z
        - ./pg_hba.conf:/var/lib/postgresql/data/pg_hba.conf
      env_file:
        - .env
    ```
7.  Edit the `redis` service, adding `healthcheck`:
    ```yaml
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 5s
    ```
    So, the entire section should look like this:
    ```yaml
    redis:
      image: redis:6-bullseye
      restart: always
      container_name: swarmica-redis
      command: redis-server --save 20 1 --loglevel warning --timeout 86400 --maxclients 100000
      healthcheck:
        test: ["CMD", "redis-cli", "ping"]
        interval: 10s
        timeout: 5s
        retries: 5
        start_period: 5s
    ```
8.  Edit the `django` service section, changing the `depends_on` sections and adding `healthcheck` like this:
    ```yaml
    django:
      ...rest of your config...
      depends_on:
        postgres:
          condition: service_healthy
        redis:
          condition: service_healthy
      healthcheck:
        test: ["CMD", "python", "-c", "import urllib.request; req = urllib.request.Request('http://127.0.0.1:5000/api/instance', headers={'Host': 'django'}); resp = urllib.request.urlopen(req, timeout=5); exit(0 if 200 <= resp.status < 400 else 1)"]
        interval: 15s
        timeout: 5s
        retries: 10
        start_period: 40s
      ...rest of your config...
    ```
9.  Next, in the services `frontend`, `helpcenter`, `celeryworker`, `celerybeat`, `django-websocket`, `postfix`,`mcp`, `telegram`, `whatsapp_green`, `vk`, `max` (in those that you have in `docker-compose.yml` - depending on your system settings, you may not have all of them) change the `depends_on` section like this:
    ```yaml
    depends_on:
      django:
        condition: service_healthy
    healthcheck:
          disable: true
    ```
10. Restart the services:
    ```shell
    docker compose down && docker compose up -d
    ```