Knowledge

Running containers as non-root and adding a HEALTHCHECK to know your app is alive

Updated 2026-11-19 · Present

  • By default the process in a container runs as the container's root; the USER instruction switches to an ordinary user.
  • HEALTHCHECK tells Docker whether the application can really serve requests or is just "running".
  • Add --read-only, --cap-drop ALL and no-new-privileges to narrow further what the container may do.

Goal

In the previous article we got a small image thanks to a multi-stage build. But a small image is not automatically safe or trustworthy. This article does two things with that same Go application:

  1. Run it as a user that is not root, so that if the application is exploited the attacker has fewer privileges.
  2. Add a HEALTHCHECK, so Docker and you know whether the application is healthy or hung even though the process has not exited.

The article builds on the sample project from Multi-stage Dockerfiles: heavy builds, light runtime images. If you have not installed Docker yet, see Installing Docker Engine on Ubuntu 24.04 from Docker's official apt repository.

Preparation

One important note: root inside a container is still a process on the real host, isolated only by Linux kernel mechanisms (namespaces, cgroups, capabilities). The isolation is good, but not invincible; that is why the principle of least privilege is still worth applying.

Steps

Steps

Step 1: See who the container runs as

With the hello:multi image from the previous article:

docker run -d --name hello -p 8080:8080 hello:multi
docker exec hello id

The result contains uid=0(root): the application runs as the container's root, because the Dockerfile has not said anything else. Stop and remove it:

docker rm -f hello

Step 2: Create a dedicated user and use `USER`

Change the last stage of the Dockerfile to:

FROM alpine:3.20
RUN addgroup -S app && adduser -S -G app app
COPY --from=build /out/hello /usr/local/bin/hello
USER app
EXPOSE 8080
ENTRYPOINT ["hello"]

addgroup -S and adduser -S are BusyBox syntax on Alpine; they create a system group and user named app, with no password. The USER app instruction makes the commands after it, and the main process at run time, use this user. Port 8080 is above 1024, so no privilege is needed to listen on it.

On other bases such as Debian or Ubuntu, the commands to create a user are groupadd and useradd; check the documentation of the base image you use.

Step 3: Add a HEALTHCHECK

Add to the last stage, after USER:

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget -q -O /dev/null http://127.0.0.1:8080/healthz || exit 1

The command after CMD runs inside the container. If it exits with code 0 the check succeeds; code 1 means it failed. The options:

Option Meaning Default
--interval How often to run the check 30 seconds
--timeout Longer than this counts as a failure 30 seconds
--start-period Start-up time during which failures are not yet counted 0 seconds
--retries Number of consecutive failures to be considered unhealthy 3

We use wget because it is already in Alpine's BusyBox, so nothing extra needs installing. If your image has no such tool, give the application itself a self-check mode, or install the small tool you need.

Step 4: Build again

docker build -t hello:safe .

Verification

Run the container:

docker run -d --name hello -p 8080:8080 hello:safe
docker exec hello id

This time the uid is not 0 and the name is app. Next, look at the health:

docker ps --filter name=hello
docker inspect --format '{{.State.Health.Status}}' hello

Right after starting, the status column shows (health: starting); after the first successful check it becomes (healthy). The inspect command prints starting, healthy or unhealthy. To see the history of the checks:

docker inspect --format '{{json .State.Health}}' hello

To see the unhealthy state, you can try changing the path in HEALTHCHECK to one that does not exist, rebuild and wait through the retries.

Narrowing permissions further at run time

Besides USER, Docker lets you restrict a container when you run it. Stop the old container and try:

docker rm -f hello
docker run -d --name hello -p 8080:8080 \
  --read-only \
  --cap-drop ALL \
  --security-opt no-new-privileges \
  hello:safe

Our small application still runs normally; call curl http://127.0.0.1:8080/ to be sure. With a real application you add the restrictions one at a time and test, because some programs need to write to disk or need a capability.

Clean up when done:

docker rm -f hello

Common errors

Summary

Two small habits make your Dockerfile much more mature: use USER so you do not run as root, and use HEALTHCHECK to know the application can really serve requests. Add --read-only, --cap-drop ALL and no-new-privileges at run time and you have noticeably narrowed what a compromised container can do. This is not the whole of container security, but it is a solid foundation to start from.

References