<!--
  canonical-url: https://www.avonture.be/blog/docker-healthy
  published:     2023-12-12
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# Get health information from your running containers

> Get health information from your running Docker containers instantly. This article shows a bash script using docker ps and docker inspect for quick checks.

---
![Get health information from your running containers](/img/v2/docker_tips.webp)

**TL;DR**

This article shares a Bash script that loops over all Docker containers (`docker container list --all`) and reports each one's health status via `docker inspect`'s `State.Health` field, giving a quick overview of which containers are healthy, unhealthy, or have no healthcheck configured (`null`).

When you've a few runnings containers on your machine, you can quickly retrieve the health information using `docker ps` and its `State.Health` response.

In this article, we'll look at how to create a bash script that can be used as a basis for other needs.

## The one-glance overview

Here is what I get on my machine, in one command:

![Docker health checks](./images/healthy.webp)

I've a few containers running, a lot are `healthy` meaning that they're running and without any issue, I've two containers in a `null` state i.e. they are sleeping and none are stopped due to an error.

That's the whole point: one screen, and you know which container needs your attention.

## The script that produces it

Please create somewhere on your disk, in a Linux console, a script f.i. called `health.sh` with this content:

**health.sh**

```bash
#!/usr/bin/env bash
# Intentionally no "errexit": one container's failed inspect shouldn't stop the
# whole list from being checked.
set -o nounset -o pipefail

GRAY=30
GREEN=32
RED=31

clear

printf "\e[1;33m%s\e[0m\n\n" "Docker containers - Health check"

docker container list --all --format "{{.Names}}" | while read -r name; do
    healthcheckStatus=$(docker inspect --format='{{json .State.Health}}' "$name" | jq -r '.Status')

    # Default color
    COLOR=${GRAY}

    if [[ "$healthcheckStatus" == "healthy" ]]; then
        COLOR=${GREEN}
    elif [[ ! "$healthcheckStatus" == "null" ]]; then
        COLOR=${RED}
    fi

    printf "%-40s\e[1;${COLOR}m%s\e[0m\n" "$name" "$healthcheckStatus"
done
```

Make sure to make the script executable: `chmod +x health.sh`, then run it with `./health.sh`.

> **Get the list of all containers:**
>
> `docker container list --all --format "{{.Names}}"` return the list of all containers and only echo the column `Name` on the console.

## Conclusion

A few lines of Bash, and the question *"is everything still fine on this machine?"* gets an answer in one look instead of a `docker inspect` per container. Don't hesitate to fine-tune the script to fit your needs.

This script gives you a snapshot, on demand. If you'd rather be **notified** when a service goes down, have a look at [Self-hosted monitoring tool](/blog/docker_uptime_kuma). And when a container is up but still unreachable from another one, [Troubleshooting for Docker containers - Accessing the other one](/blog/docker-networking-troubleshooting) walks through the diagnosis, layer by layer.
