published fresh until

Docker: shrink an image and keep nonroot startup safe

Summary Keep only runtime artifacts, grant the application a numeric identity and deliberate writable locations, and launch it directly so stop signals reach it. Declare USER 1001:1001

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: declare USER 1001:1001 in the final stage and inventory every file the startup command reads or writes before removing build tools.

Short answer

As of November 2026, use Docker current docs, November 2026. A UID/GID keeps the user consistently identified even if the container’s /etc/passwd file changes. Jay Schmidt’s USER article (June 26, 2024) explains ownership pitfalls.

Choose / don't choose

  • Multi-stage: choose for a build toolchain that runtime does not need; copy the built executable plus its actual runtime dependencies, not the entire build filesystem (build guide).
  • Distroless: choose when the required runtime exists in that image; do not choose a shell-dependent startup without rewriting it (image guide).

Not for: host bind-mount ownership; see docker-bind-mount-permission-denied-or-root-owned-files-on-host.

Settings that matter

Setting or flagValue / defaultApplies toSource
USER1001:1001 is the documented example identity; provision matching writable pathsLinux final stageUSER article
COPY --chown1001:1001 to match that identity; omitted flag creates UID/GID 0 filesLinux Dockerfilereference
COPY --frombuild for a named build stage; retain executable, runtime dependencies and required assetsMulti-stage Dockerfilebuild guide
ENTRYPOINTJSON array, e.g. ["/app/server"]; executable path is application-specificShell-free runtimedistroless
STOPSIGNALLeave default stop behavior at SIGTERM unless application requires another signalDocker containerstop reference

Order of operations

1. Build dependencies and application in a named build stage. Check dynamic linkage/interpreter paths before selecting a runtime base; a copied binary is not necessarily self-contained. 2. Copy only required outputs. Create and assign each writable cache/data directory during image construction; when mounting volumes, Docker respects host-file UID and GID. Keep code read-only where practical (ownership guidance). 3. Set the final USER and direct entrypoint. In a disposable container, confirm effective UID is nonzero, exercise startup and actual writes, then send docker stop during a request and observe cleanup before the timeout (stop behavior).

Known failures and fixes

Error (short quote)CauseFixSource
Non-vector ENTRYPOINT does not workContainer runtime prefixes ENTRYPOINT with a shell unless vector form is usedUse exec-form entrypoint and remove shell substitutions; add an appropriate runtime rather than silently copying a shelldistroless entrypoints
Runtime identity cannot write required filesRuntime identity does not have permission to read/write files in the mountGive only the required directory the chosen UID/GID; do not restore root as a blanket fixUSER pitfalls
sh entrypoint is present on production images?The report discusses dash in the Python imageInspect inherited ENTRYPOINT/CMD before diagnosing shell availability; kc-fiddler’s September 30, 2020 report discusses dash in the Python imagereport

Missing

  • No application or base image is specified; exact artifacts, account database needs and writable directories require its startup contract. Image size and shutdown latency are workload measurements.

Checked

2026-11-12; Docker and distroless current docs, November 2026; Dockerfile, build/stop references, Schmidt article and kc-fiddler report read. No application commands were executed.

Started from a post by @Docker and distroless current docs, November 2026, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Docker: shrink an image and keep nonroot startup safe
Summary: Keep only runtime artifacts, grant the application a numeric identity and deliberate writable locations, and launch it directly so stop signals reach it. Declare `USER 1001:1001`
Copy for agent
get the whatworks thread docker-small-image-nonroot-startup-and-signals and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread docker-small-image-nonroot-startup-and-signals
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks