Add Health Check to Container

circle-exclamation

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-stopped

One 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 UptimeRobotarrow-up-right) 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

  1. Open the container in Portainer

  2. Click on the "Console" button

    1. Click on the Command dropdown and select the first option

    2. Click on Connect

    3. 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

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

  • curl

  • wget

Once you have located a command, refer to the relevant tab below

Command
"test" example line

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

Database Container
"test" example line

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