Lab 161 · containers

Healthchecks (HEALTHCHECK + docker ps)

Add a Dockerfile HEALTHCHECK so operators can see container health directly in docker ps. Build, run, verify the health status, and clean up safely.

docker ops reliability

Scenario

You are on-call for a small internal platform that runs in Docker. A recent incident showed containers can appear Up while the application inside is non-functional. You need containers to self-report health so responders can triage faster using standard tooling.

Operator context

During incidents, you do not want to SSH into hosts to guess what is broken. Health status in docker ps gives an immediate signal you can act on before deeper debugging.

Objective

  • Create an isolated working directory for the lab.
  • Write a Dockerfile that includes a HEALTHCHECK.
  • Build an image and run a container from it.
  • Verify health status using docker ps output formatting.
  • Clean up containers, images, and lab artifacts.

Concepts

  • Container liveness versus application health: Up does not mean usable.
  • Dockerfile HEALTHCHECK as an operator-facing signal.
  • Health status visibility through docker ps and structured output (--format).
  • Safe cleanup: stop/remove containers first, then remove images, then remove directories.

Walkthrough

Step 1: Create a working directory and enter it.
Command
mkdir -p /root/healthcheck-lab && cd /root/healthcheck-lab

Use a dedicated directory so your Dockerfile and build context are isolated. This avoids unintentionally building with extra files or leaving artifacts behind.

Step 2: Create a Dockerfile with a HEALTHCHECK using a heredoc.
Command
cat > Dockerfile << 'EOF'

The heredoc writes the Dockerfile in one operation. The healthcheck below is intentionally simple and always returns success, so you can validate the signal path first.

FROM busybox:latest
# Simple container that stays running
CMD ["sh", "-c", "while true; do sleep 2; done"]
# Self-reporting health: always succeeds (simulates a passing check)
HEALTHCHECK --interval=5s --timeout=2s --start-period=3s --retries=3 \
  CMD sh -c 'exit 0'
EOF
Step 3: Build the image.
Command
docker build -t hc-demo:1 .
  [+] Building 3.1s (6/6) FINISHED
  => => naming to docker.io/library/hc-demo:1

Tag the image so it is easy to reference during verification and cleanup.

Step 4: Run the container in detached mode.
Command
docker run -d --name hc1 hc-demo:1
  8c2d7b0e2a6f7c8a0b5a2b1c9f2a3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c

A detached container returns a container ID. The healthcheck will begin evaluating after the start period and transition the status once Docker records results.

Step 5: Verify health status via docker ps formatting.
Command
docker ps --format '{{.Names}} {{.Status}}'
  hc1 Up 12 seconds (healthy)

This is the operator-facing outcome: a quick, scriptable view of container health without digging into logs or entering the container.

Step 6: Stop and remove the container.
Command
docker rm -f hc1
  hc1

Remove the container first so the image can be deleted cleanly.

Step 7: Remove the image.
Command
docker rmi hc-demo:1
  Untagged: hc-demo:1
  Deleted: sha256:3d9c1c0b7a9e...

If the image is still referenced by a container, removal will fail. That is usually a signal that cleanup order is wrong.

Step 8: Remove the lab directory.
Command
rm -rf /root/healthcheck-lab

Clean build context artifacts so your environment stays predictable between lab runs.

Common breakpoints

Health status stays “starting”

The check may not have executed yet. Wait past the start period, then re-run docker ps to confirm the status transitions.

docker build fails: Dockerfile not found

Confirm you are in the correct directory and the file is named exactly Dockerfile. Build context is the current directory when you use ..

Container name conflict: “hc1 is already in use”

Remove the existing container (docker rm -f hc1) or run with a new name using --name.

docker rmi fails: image is being used by a container

A stopped container can still reference an image. Remove containers first, then remove the image.

Cleanup checklist

This lab creates a container, an image, and a working directory. If anything failed mid-run, use the commands below to return the host to a clean baseline.

Commands
docker rm -f hc1
docker rmi hc-demo:1
rm -rf /root/healthcheck-lab

Reference

  • mkdir -p <dir>: Creates a directory path (including parents if needed).
    • -p: Creates parent directories and avoids errors if the directory exists.
  • cd <dir>: Changes the current working directory.
  • cat > Dockerfile << 'EOF': Writes a file using a heredoc.
    • >: Redirects output into a file (overwrites existing content).
    • << 'EOF': Reads input until the EOF marker; quotes prevent variable expansion.
  • docker build -t <tag> .: Builds an image from a Dockerfile in the current directory.
    • -t: Assigns a name and optional tag to the image (for example hc-demo:1).
    • .: Uses the current directory as the build context.
  • docker run -d --name <name> <image>: Starts a container from an image.
    • -d: Runs the container in detached mode.
    • --name: Assigns a predictable container name.
  • docker ps --format '{{.Names}} {{.Status}}': Lists running containers with custom output formatting.
    • --format: Uses Go templates to select fields like .Names and .Status.
  • docker rm -f <name>: Removes a container (force stops it if running).
    • -f: Forces removal by stopping the container first.
  • docker rmi <image>: Removes an image from the local image store.
  • rm -rf <path>: Removes files/directories recursively and forcefully.
    • -r: Removes directories and their contents recursively.
    • -f: Ignores non-existent files and suppresses prompts.