Dockerfile Multi-Stage Builds, Non-Root Runtime and Health Checks

Open Tool Dockerfile Generator

Dockerfile Multi-Stage Builds, Non-Root Runtime and Health Checks

When to use

Use this generator when a project already runs locally and needs its first container image, or when a single-stage image has grown too heavy. The typical case is a web service with a build step, a runtime step and one writable directory.

It covers Node.js, Python, Go, PHP, Java and static sites. Each runtime starts with its own base image, port and start command. I treat the result as the first commit of an image definition, then adjust it against the repository scripts.

It is not a Kubernetes manifest generator, a Compose orchestrator or a database bootstrap tool. Those need networking, storage and startup ordering that a single Dockerfile cannot express.

Steps

  1. Choose a runtime. Node.js starts with node:20-alpine, port 3000, npm run build and npm start.
  2. Edit the base tag, port, build command, start command and runtime user. Pin the tag to at least 20-alpine; do not leave latest.
  3. In the advanced card, pick the Node package manager, enter a health path and image name, then list NAME or NAME=VALUE entries one per line.
  4. Keep multi-stage, non-root, health check, BuildKit cache and OCI labels enabled, and read the self-check list.
  5. Switch to the .dockerignore tab, add the real build output folder, then copy or download both files.

Screenshots

Default state: parameters on the left, generated file and checks on the right

This is the first screen. The left column holds runtime and advanced build controls; the right column holds the preview and copy actions.

A real interaction: loading the Go sample updates the preview

After I clicked the sample button and switched to Go, the preview gained a golang:1.22-alpine build stage and a distroless runtime stage.

Preview detail: multi-stage copies, USER and HEALTHCHECK

The last image focuses on the preview. Check COPY --from, USER and HEALTHCHECK in that order; hand edits often move one of them.

Features in detail

Runtime switching changes more than an image name. Node produces a dependency stage, a build stage and a runtime stage. Python creates /opt/venv and copies the virtual environment. Go compiles a static binary and runs it on distroless. PHP installs Composer dependencies first. Java separates Maven from the JRE. A static site is built with Node and served by Nginx.

The Node package manager is wired into the file. npm uses npm ci, pnpm uses pnpm install --frozen-lockfile, and yarn uses yarn install --immutable. Each option copies a different lock file and uses a different cache directory. A mismatch fails on the first build.

BuildKit cache mounts are optional and on by default. npm caches under /root/.npm, pnpm under /root/.local/share/pnpm/store, pip under /root/.cache/pip, Go under both the module and build caches, Composer under /tmp/composer-cache, and Maven under /root/.m2. These mounts never enter the final image.

OCI labels add three fields: title, description and license. The title comes from the image name, the description says the file was generated here, and the license defaults to MIT. Labels do not affect runtime behavior, but they make registry metadata complete.

ARG lines accept NAME or NAME=VALUE; a value becomes the Dockerfile default and is visible in the file, so never put secrets there. Pass sensitive values through CI --build-arg flags or secrets at build time. With BuildKit cache enabled the first line is # syntax=docker/dockerfile:1.7; every runtime stage gets a STOPSIGNAL; and .dockerignore excludes .pem, .key, id_rsa* and similar credential files. The self-check also reports an empty start command, a floating tag, a root user or an out-of-range port.

What you get

The input is eight text or numeric values: runtime, base tag, port, build command, start command, runtime user, health path and image name. Five switches control multi-stage mode, non-root mode, the health check, BuildKit cache and OCI labels.

The output is two plain-text files. Dockerfile contains FROM, WORKDIR, COPY, RUN, ENV, USER, LABEL, EXPOSE, HEALTHCHECK, and a final CMD or ENTRYPOINT. .dockerignore removes dependencies, caches, logs and local configuration for the chosen runtime.

Copy writes the current tab to the clipboard. Download names the file correctly: Dockerfile has no .txt suffix and .dockerignore keeps its leading dot. Neither action performs a network request.

Parameters

ControlTypeDefaultNotes
RuntimeselectNode.jsSix options with matching defaults
Base tagtext20-alpinePin a major version and variant
Container portnumber30001–65535; invalid values fall back
Build commandtextnpm run buildWritten into a RUN line
Start commandtextnpm startConverted into a JSON-array CMD
Runtime usertextappInvalid characters removed; root falls back to app
Node package managerselectnpmNode only; changes lock file and cache
Health pathtext/healthMust start with /, otherwise it falls back
Image nametextappUsed in the build hint and OCI title
ARG namestextareaemptyNAME or NAME=VALUE per line; values become ARG defaults

Self-check list

  • □ The number of FROM lines matches the stage switch: usually three for multi-stage, one or two otherwise.
  • □ The final stage has a USER, and the name is not root.
  • □ EXPOSE matches the port the start command actually listens on.
  • □ HEALTHCHECK hits a path the app answers; Go needs a binary that supports -healthcheck.
  • □ Dependency manifests are copied before source so the dependency layer can be reused.
  • □ .dockerignore excludes .env, logs, local dependencies and build output.

FAQ

Question: Will docker build succeed right after generation? Answer: The structure is valid, but dependency versions, system libraries and project scripts still need a real build. Add --progress=plain on the first run.

Question: Why does the Node runtime still run npm prune? Answer: The build stage carries full dependencies. Pruning removes test and bundling tools, which makes the final image smaller. pnpm and yarn use their own production commands.

Question: What if a non-root user cannot write logs? Answer: Create the directory and chown it during the build, or log to standard output. Do not switch back to root for convenience.

Question: Why is the Go health check not HTTP? Answer: Distroless has no shell and no wget. The sample calls a -healthcheck flag in the binary; implement it or use an external probe.

Question: Do BuildKit cache mounts end up in the image? Answer: No. --mount=type=cache exists only during the build and is absent from the final layers.

Limits

The plugin does not run docker build or detect a Docker daemon. It does not handle private registry login, cross-platform build matrices, Kubernetes probes or Compose dependencies.

Single-stage mode still installs dependencies and runs commands, but it does not split out compilers. For extreme size reductions, adjust the stage boundary by hand.

PHP Apache and PHP-FPM images start differently. With a non-Apache image, change the start command and port, or the container may never listen on the expected socket.

Design trade-offs

The generator builds strings with arrays, one function per runtime, and joins them with \n. Cost grows with output lines; a 30-line file finishes inside one event loop turn and makes no network calls.

I did not add a template engine. A template adds a dependency and makes conditional stages harder to read. The trade-off is longer source code, but every FROM and RUN is easy to locate.

Continuous input is coalesced with requestAnimationFrame, and identical state signatures skip a redraw. Typing a port or pasting ARG names produces at most one render per frame.

Troubleshooting

Symptom: the build fails with COPY failed: file not found. The usual cause is a missing lock file or a .dockerignore rule that hides the manifest. Check the source path in the error, then the ignore rules.

Symptom: the container exits immediately. The start command was split at the wrong space, especially for nginx -g "daemon off;". Inspect the generated JSON array and confirm daemon off; is one argument.

Symptom: the Go image stays unhealthy. Distroless has no wget, and the sample calls a flag the app may not implement. Add the flag or remove HEALTHCHECK and use an external monitor.

Diagnostic order: first FROM and stage names, then COPY source paths, then USER, EXPOSE and the start command. This order finds most failures within a minute.

Workflow and acceptance

Roll out in stages: generate a single-stage file on a test branch, get it running, then enable multi-stage, then non-root, then the health check. Each step can be reverted on its own.

For team work, commit the Dockerfile and .dockerignore together. Put the image name, port and health path in the README so nobody keeps a private variant.

Acceptance: docker build -t app . succeeds; docker run --rm -p 3000:3000 app answers; docker inspect shows a non-root User; the health check turns healthy within 30 seconds; removing one ARG still builds.

Version and compliance

This release is 1.1.4 and follows ToolCMS plugin spec 3.19. The page loads no external scripts, uploads no input and does not contact a Docker daemon. Every result is produced in the local browser.

The output is a starting point, not a finished image. Run dependency scanning, image signing and registry permission checks before production. The default license is MIT; change the OCI label when the project uses another license.

⏫︎