Add Health Check to Container
THIS GUIDE IS A WORK IN PROGRESS
Time Required
5 Minutes
Difficulty
Low - Moderate
Required Knowledge
SSH, Docker, Docker Compose
The easiest way to add health checks to your docker containers is to update the Compose file, below you will find some examples. The Health Check needs to be added at the same indentation (how many 'TAB' keys) as your 'image', 'volume' and/or 'environment' lines, per below
version: "2.1"
services:
app:
image: ghcr.io/linuxserver/plex:1.32.6
network_mode: host
volumes:
- app:/config
- media:/media
healthcheck:
test: curl --connect-timeout 30 --silent --show-error --fail http://localhost:32400/identity
interval: 1m
timeout: 30s
retries: 3
start_period: 1m
restart: unless-stoppedOne thing to note is that not all containers will need health checks. Health checks do require additional compute to do and it may be better using a cloud service (such as UptimeRobot) or an internally hosted service like UptimeKuma
Add a Health Check to your Compose file
Please read the code you are adding and ensure your variables match, otherwise you will have issues!
Access the CLI of your container
Open the container in Portainer
Click on the "Console" button
Click on the Command dropdown and select the first option
Click on Connect
Repeat until you are connected
Example failed to connect message: OCI runtime exec failed: exec failed: unable to start container process: exec: "bash": executable file not found in $PATH: unknown
Once you are connected, go to the relevant health check section below
SSH into your docker server
Docker the command d
ocker psto list your docker containersTake note of the containers listed and their names - locate the container name you wish to add a health check to
Run the below command to attempt to connect to the container
docker exec -it <containername> <shelltype>Repeat step 3 for each shell type
Shell types:
/bin/bash/bin/ash/bin/dash/bin/sh
Once you are connected, go to the relevant health check section below
Health Checks
Web page
Run the follow commands and take note of the output. If you get a "command not found" or something similar, you cannot use that command for your health check
curlwget
Once you have located a command, refer to the relevant tab below
Curl
curl --connect-timeout 15 --silent --show-error --fail http://localhost:<PORT>
Wget
wget --no-verbose --tries=1 --spider http://LOCALHOST:<PORT> -O /dev/null || exit 1
If neither are available, try
["CMD-SHELL", "/bin/bash -c 'echo > /dev/tcp/127.0.0.1/8080 || exit 1'"]
Database
MySQL
["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u ${MYSQL_USER} -p ${MYSQL_PASS} --silent"]
MariaDB
PostGres
["CMD-SHELL", "pg_isready -U n8n -d n8n || exit 1"]
Last updated