Ship a small image that runs unprivileged and reports its health

Packaging servicesAbout 30 minDraft

Not started.

What you need

Windows 11 with Docker Desktop running, and either PowerShell or a WSL terminal. The verifier runs inside a throwaway Node container, so nothing else needs installing.

Steps

  1. Save the two starting files into an empty folder, together with verify.mjs.
  2. Build the image as it is: docker build -t skilltree/hello:1 . and run the verifier. It fails, and says why.
  3. Change the Dockerfile so the server runs as a non-root user, and add a HEALTHCHECK that asks the server for its page. Keep the image under 200 MB.
  4. Build again with the same tag and run the verifier until every check passes. Paste its last line below.

Why it matters

A container that runs as root is root inside its namespace. If the process is compromised, or a volume is mounted from the host, root makes every mistake bigger. Create a user with a fixed ID in the image and switch to it with USER before the command runs.

Docker only knows that a process is running, not that it serves anything. A HEALTHCHECK runs a command inside the container on a timer; Compose, Swarm and your own scripts can then wait for "healthy" instead of guessing. Alpine images have wget from BusyBox, so a request to the page needs nothing extra.

The verifier starts a container from your image, waits up to a minute for Docker to report it healthy, then removes it.

When you are done

  • docker rmi skilltree/hello:1

Files

Show Dockerfile
# Starting point for the hardened-image task. It builds and serves the page, but it runs as root and Docker has no
# way to tell whether it is healthy. Fix both, keep it small, and build it as skilltree/hello:1.
FROM python:3.12-alpine
WORKDIR /srv
COPY index.html .
EXPOSE 8080
CMD ["python", "-m", "http.server", "8080"]
Show index.html
<!doctype html><title>hello</title><p>Hello from a container.</p>

It asks Docker for what it needs and changes nothing else; read it before you run it. Mounting the Docker socket gives it the same control over Docker that you have.

Run the verifier

In the folder with your files, in PowerShell or in WSL:

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v "${PWD}:/work" -w /work node:22-alpine node verify.mjs hardened-image

The verifier runs on your machine, so this page cannot see the result for itself: what you paste is what it records.

Checks

Checks

  • The image skilltree/hello:1 existsnot yet checked
  • It runs as a non-root usernot yet checked
  • It has a health checknot yet checked
  • It is under 200 MBnot yet checked
  • A container from it becomes healthynot yet checked
Esc
↑ ↓ to move↵ to open