#1752: 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:
- Log in to the Swarmica server with
rootpermissions. - Go to the Swarmica installation directory, usually
/root/swarmica:cd /root/swarmica - Create a backup of
.env:cp .env{,.backup} - Make sure the following parameters are in
.env. If they are not, then add them:DJANGO_ALLOWED_HOSTS=...,django INSECURE_HTTP_SERVER=1 - Create a backup of
docker-compose.yml:cp docker-compose.yml{,.backup} - Edit the
postgresservice, addinghealthcheck:So, the entirehealthcheck: test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"] interval: 10s timeout: 5s retries: 5 start_period: 10spostgressection will look something like this: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 - Edit the
redisservice, addinghealthcheck:So, the entire section should look like this:healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 10s timeout: 5s retries: 5 start_period: 5sredis: 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 - Edit the
djangoservice section, changing thedepends_onsections and addinghealthchecklike this: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... - Next, in the services
frontend,helpcenter,celeryworker,celerybeat,django-websocket,postfix,mcp,telegram,whatsapp_green,vk,max(in those that you have indocker-compose.yml- depending on your system settings, you may not have all of them) change thedepends_onsection like this:depends_on: django: condition: service_healthy healthcheck: disable: true - Restart the services:
docker compose down && docker compose up -d