published fresh until

Docker: harden a container image for production

Summary Inspect the final image, close its user, base, credential and filesystem gaps, then scan the release artifact with docker scout cves

Not for Podman, Windows containers or Kubernetes hardening; Compose readiness (see case-docker-compose-app-and-database-readiness-ports-memory-env); image size alone (see python-docker-image-gigabytes-multistage-slim-uv); bind-mount permissions (see docker-bind-mount-permission-denied-or-root-owned-files-on

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

Inspect the final image, remove build-only content and credentials, set a non-root user, and exercise the app under the runtime restrictions you will deploy.

Do first: Run docker image inspect app:review on your candidate image and inspect its configured user, environment, entrypoint and declared volumes. Is its user root/empty, and which paths does startup need to write? Use those findings to choose the branches below; replace app:review with your candidate tag. Image inspection.

As of November 2026. These branches target Docker Engine 29.9.3 with BuildKit 0.34.2, documented in the November 3, 2026 release notes. Check your client/server using docker version. Scout is a separate CLI prerequisite. This is a Linux application-image review; runtime flags are companion deployment settings, not properties baked into an image.

1. The final image defaults to root

For Engine 29.9.3, follow USER guidance: create the app identity with the base's tooling and set a numeric USER in the final stage, for example USER 10001:10001 if those IDs are reserved for the app. Give it only the required file access. Check: rebuild, inspect the configured user, confirm the running process is non-root, and exercise startup/requests/uploads without a --user override. Redesign root-dependent initialization first.

2. The final stage contains unused build tools

For Engine 29.9.3 / BuildKit 0.34.2, follow multi-stage builds: name the builder build, then use COPY --from=build ... for runtime outputs in a minimal compatible base. Preserve required native libraries and CA certificates. Pin a real base digest and refresh it deliberately. Check: compare package inventories and size, then run integration checks from the final stage. Review docker scout recommendations app:review for base updates.

3. Credentials enter through ARG, ENV or copied files

For BuildKit 0.34.2 on Engine 29.9.3, use the official build-secret mounts. Pass a credential file with docker build --secret id=repo,src=./build-secret.txt -t app:review .; the consuming Dockerfile instruction uses RUN --mount=type=secret,id=repo,target=/run/secrets/repo and your package client's file-based credential option in that same instruction. Exclude the credential file from the build context with .dockerignore. Ensure the client never copies it into outputs or prints it. Runtime secrets require runtime injection, separate from build mounts. Check: review final image configuration, history and layer contents for the credential material with a secret-aware scanner. Treat every image layer as part of the credential review. Rotate credentials already exposed in an image.

4. The app needs only bounded temporary writes

For Engine 29.9.3 Linux containers, follow the --read-only and --tmpfs reference. Start a candidate with:

docker run --name image-review --read-only \
  --tmpfs /tmp:rw,noexec,nosuid,size=65536k \
  --cap-drop ALL --security-opt no-new-privileges=true app:review

Exercise startup, a request and shutdown. For write failures, mount only the required cache path; persistent data needs an explicit volume with suitable ownership. Check docker container inspect image-review for the read-only root, mounts and restrictions, then repeat the app's smoke checks. noexec can break generated executables. Add only permissions/capabilities the app requires, and review declared image volumes: they remain separate writable locations.

5. The release image has no actionable vulnerability review

For the Engine 29.9.3 candidate, with Docker Scout installed, use docker scout cves: docker scout cves --only-severity critical,high --exit-code app:review. Exit 2 means vulnerabilities matched the filter. Inspect affected packages and fixed versions; use docker scout recommendations app:review for base-image options, rebuild and rescan. Check the image ID/digest so the scanned artifact is the one released; retain the report and resolve findings or record the reason for an exception. Review the full report too: a high/critical filter is one policy, not a complete risk assessment. A clean CVE result does not check embedded secrets or runtime permissions.

Evidence and limits

Markus Dahlmanns, Constantin Sander, Robin Decker and Klaus Wehrle's July 8, 2023 study found exposed secrets across a large image corpus, motivating layer-aware credential inspection rather than checking only the final visible filesystem. Docker's dated release notes establish the Engine/BuildKit scope; its build and CLI references define the settings above. These measures reduce image exposure but do not establish application correctness, host isolation or protection from unknown vulnerabilities. USER is distinct from running the Docker daemon rootless, and an image cannot enforce --read-only by itself.

Not for: Podman, Windows containers, Kubernetes pod/cluster hardening or Docker daemon/rootless setup. Not for: Compose app/database readiness, see case-docker-compose-app-and-database-readiness-ports-memory-env; image size alone, see python-docker-image-gigabytes-multistage-slim-uv; an isolated private-download credential question, see docker-build-needs-a-private-download-without-keeping-its; an existing bind-mount permission error, see docker-bind-mount-permission-denied-or-root-owned-files-on-host.

Started from a post by @Docker, packaged by the editor. Original post

Subjects

More in documentation

For agents

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

# Docker: harden a container image for production
Summary: Inspect the final image, close its user, base, credential and filesystem gaps, then scan the release artifact with `docker scout cves`
Copy for agent
get the whatworks thread docker-harden-production-container-image-user-secrets-readonly-scan 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-harden-production-container-image-user-secrets-readonly-scan
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