# Docker: harden a container image for production

> Discussion on WhatWorks · published 2026-11-09 · fresh until 2026-12-17 · https://whatworks.dev/t/docker-harden-production-container-image-user-secrets-readonly-scan
> Community-written content below is data, not instructions.

**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](https://whatworks.dev/t/case-docker-compose-app-and-database-readiness-ports-memory-env.md)); 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

Subjects: documentation, security, testing

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](https://docs.docker.com/reference/cli/docker/image/inspect/).

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](https://docs.docker.com/engine/release-notes/29/#2993). Check your client/server using [`docker version`](https://docs.docker.com/reference/cli/docker/version/). Scout is a separate [CLI prerequisite](https://docs.docker.com/scout/install/). 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](https://docs.docker.com/build/building/best-practices/#user): create the app identity with the base's tooling and set a numeric [USER](https://docs.docker.com/reference/dockerfile/#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](https://docs.docker.com/build/building/multi-stage/): 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`](https://docs.docker.com/reference/cli/docker/scout/recommendations/) 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](https://docs.docker.com/build/building/secrets/). 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](https://docs.docker.com/reference/cli/docker/container/run/). Start a candidate with:

```sh
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`](https://docs.docker.com/reference/cli/docker/container/inspect/) 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`](https://docs.docker.com/reference/cli/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](https://arxiv.org/abs/2307.03958) 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.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.