# Add Health Check to Container

{% hint style="warning" %}
THIS GUIDE IS A WORK IN PROGRESS
{% endhint %}

<table data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td>Time Required</td><td>5 Minutes</td></tr><tr><td>Difficulty</td><td>Low - Moderate</td></tr><tr><td>Required Knowledge</td><td>SSH, Docker, Docker Compose</td></tr></tbody></table>

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

<pre class="language-yaml" data-full-width="false"><code class="lang-yaml">version: "2.1"
services:
  app:
    image: ghcr.io/linuxserver/plex:1.32.6
    network_mode: host
    volumes:
      - app:/config
      - media:/media
<strong>    healthcheck:
</strong><strong>      <a data-footnote-ref href="#user-content-fn-1">test: curl --connect-timeout 30 --silent --show-error --fail http://localhost:32400/identit</a>y
</strong><strong>      <a data-footnote-ref href="#user-content-fn-2">interval: 1m</a>
</strong><strong>      <a data-footnote-ref href="#user-content-fn-3">timeout: 30s</a>
</strong><strong>      <a data-footnote-ref href="#user-content-fn-4">retries: 3</a>
</strong><strong>      <a data-footnote-ref href="#user-content-fn-5">start_period: 1m</a>
</strong>    restart: unless-stopped
</code></pre>

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 [UptimeRobot](https://uptimerobot.com/)) 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

{% tabs %}
{% tab title="Portainer" %}

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`&#x20;

**Once you are connected, go to the relevant health check section below**
{% endtab %}

{% tab title="SSH" %}

1. SSH into your docker server
2. Docker the command d`ocker ps` to list your docker containers
3. Take note of the containers listed and their names - locate the container name you wish to add a health check to
4. Run the below command to attempt to connect to the container\
   `docker exec -it <containername> <shelltype>`
5. 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**

[More information over on the docker docs](https://docs.docker.com/reference/cli/docker/container/exec/)
{% endtab %}
{% endtabs %}

### 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`&#x20;

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

<table><thead><tr><th width="182">Command</th><th>"test" example line</th></tr></thead><tbody><tr><td>Curl</td><td>curl <a data-footnote-ref href="#user-content-fn-6">--connect-timeout 15</a> --silent --show-error --fail <a data-footnote-ref href="#user-content-fn-7">http://localhost:&#x3C;PORT></a></td></tr><tr><td>Wget</td><td>wget --no-verbose --tries=1 --spider <a data-footnote-ref href="#user-content-fn-8">http://LOCALHOST:&#x3C;PORT></a> -O /dev/null || exit 1</td></tr><tr><td>If neither are available, try</td><td>["CMD-SHELL", "/bin/bash -c 'echo > /dev/tcp/127.0.0.1/<a data-footnote-ref href="#user-content-fn-9">8080</a> || exit 1'"]</td></tr></tbody></table>

### Database

<table><thead><tr><th width="182">Database Container</th><th>"test" example line</th></tr></thead><tbody><tr><td>MySQL</td><td>["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u ${MYSQL_USER} -p ${MYSQL_PASS} --silent"]</td></tr><tr><td>MariaDB</td><td></td></tr><tr><td>PostGres</td><td>["CMD-SHELL", "pg_isready -U n8n -d n8n || exit 1"]</td></tr></tbody></table>

[^1]: Health Check Command

[^2]: How often to check

[^3]: How long the command should wait for an output. If no point (eg timed out), it will be marked towards the retry count. Eg 3 time outs = unhealthy

[^4]: How many retries before marking container as unhealthy

[^5]: How long to wait after starting container

[^6]: how long to wait before timing out

[^7]: you will need to set the port at minimum

[^8]: You will need to se the port and potentially hostname

[^9]: Update port


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.trentbauer.com/guides/other-guides/docker-compose/add-health-check-to-container.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
