Learning center

Does a Dockerfile root-user finding mean the container runs as root?

It does when the final build stage sets no non-root USER and the deployment that runs the image does not override the user. A linter reads the Dockerfile, and checkov's checks read the whole file rather than the stage that becomes the image. Check the final stage yourself, then check every workload that runs the image.

Two rules decide the user#

The Dockerfile reference scopes USER to one stage: it sets the default user "for the remainder of the current stage", and "Each FROM instruction clears any state created by previous instructions." (Dockerfile reference, BuildKit dockerfile/1.12.0) In a multi-stage build, the stage that becomes the image is the only one whose USER counts; for an ordinary build that is the last stage. The image records it in its configuration's User field, which the OCI image specification says "acts as a default value to use when the value is not specified when creating a container." (Image configuration, image-spec v1.1.0)

The runtime decides the rest. The Docker run reference states: "The default user within a container is root (uid = 0)", and it says the -u option overrides the USER instruction (docker run reference, docker/cli v27.3.1). Kubernetes documents runAsUser as "The UID to run the entrypoint of the container process. Defaults to user specified in image metadata if unspecified." (core/v1 types.go, kubernetes/api v0.31.2)

So the evidence is the final stage, the built image's User field when you can inspect it, and the security settings of each workload. No Docker daemon was available for this guide, so the images below were not built; the reasoning about their user follows the Dockerfile rules quoted above.

What checkov reports on five multi-stage files#

These Dockerfiles are constructed. Each builds a Go binary in a golang:1.24.7 stage and copies it into a debian:12.7-slim final stage. The first has no USER at all:

FROM golang:1.24.7 AS build
WORKDIR /src
COPY . .
RUN go build -o /out/api ./cmd/api

FROM debian:12.7-slim
COPY --from=build /out/api /usr/local/bin/api
HEALTHCHECK CMD ["/usr/local/bin/api", "-health"]
ENTRYPOINT ["/usr/local/bin/api"]

checkov 3.4.11, installed from PyPI, flags it:

$ checkov --skip-download --compact --framework dockerfile --check CKV_DOCKER_3,CKV_DOCKER_8 -f no-user/Dockerfile
Passed checks: 0, Failed checks: 1, Skipped checks: 0
Check: CKV_DOCKER_3: "Ensure that a user for the container has been created"
	FAILED for resource: no-user/Dockerfile.

The finding is right: the final stage sets no user, so the image runs as root by default. The other four files differ mainly in where USER appears. builder-only adds RUN useradd --create-home builder and USER builder to the build stage (building under /home/builder) and still sets no USER in the final stage. final-user adds RUN useradd --uid 10001 --no-create-home app and USER 10001 to the final stage. final-uid0 and final-root replace that last line with USER 0 and USER root. The same command on each:

DockerfileFinal stage's userCKV_DOCKER_3CKV_DOCKER_8
no-userroot (no USER)FAILEDnot reported
builder-onlyroot (no USER in the final stage)PASSEDPASSED
final-user10001PASSEDPASSED
final-uid0root (UID 0)PASSEDPASSED
final-rootrootPASSEDFAILED, at line 9

Two rows pass both checks while the image would run as root. The check sources at the same tag show why. CKV_DOCKER_3 returns CheckResult.PASSED as soon as it finds any USER instruction in the file, in any stage (UserExists.py, checkov 3.4.11). CKV_DOCKER_8, "Ensure the last USER is not root", takes the last USER in the file and fails only when its value is the string root (RootUser.py, checkov 3.4.11), so USER 0 passes.

This matters for the decision in both directions. A fix that adds USER to the build stage clears the finding and leaves the image unchanged, so review the fix by reading the final stage, not by rerunning the scan. And a clean scan of a multi-stage file is no evidence that the image runs as a non-root user.

The disconfirmation: an override in one workload only#

A common reason to dismiss the finding is that the deployment sets the user. That is true only for the workloads that set it. In this constructed example the no-user image, registry.example.com/api:1.8.0, runs in two places. The Deployment sets a pod security context:

    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 10001
      containers:
        - name: api
          image: registry.example.com/api:1.8.0

A nightly CronJob, added later for a reindex task, uses the same image with no security context:

          containers:
            - name: reindex
              image: registry.example.com/api:1.8.0
              args: ["-reindex"]

checkov's Kubernetes check sees the difference:

$ checkov --skip-download --compact --framework kubernetes --check CKV_K8S_23 -d k8s
Passed checks: 1, Failed checks: 1, Skipped checks: 0
Check: CKV_K8S_23: "Minimize the admission of root containers"
	PASSED for resource: Deployment.prod.api
	File: /k8s/api-prod.yaml:1-19
Check: CKV_K8S_23: "Minimize the admission of root containers"
	FAILED for resource: CronJob.prod.api-reindex
	File: /k8s/api-batch.yaml:1-16

No cluster was available here, so the manifests were scanned and not run. Under the documented defaults, the Deployment's container runs as UID 10001 and the CronJob's falls back to the image's user, which is root. The same field documentation says what runAsNonRoot does on its own: "If true, the Kubelet will validate the image at runtime to ensure that it does not run as UID 0 (root) and fail to start the container if it does. If unset or false, no such validation will be performed." Set without runAsUser, it would stop the no-user image from starting, which surfaces the problem instead of fixing it.

To dismiss the Dockerfile finding on the strength of a runtime override, list every workload, script and docker run invocation that references the image, and confirm each one sets a non-root user. One that does not, like the CronJob here, makes the finding real.

Decide from the final stage and each runtime#

What the finding saysEvidence to collectDecision
No USER in the DockerfileThe final stage; the built image's configured userFix: add a non-root USER to the final stage
Scan passes, multi-stage fileWhich stage holds the last USER, and its value (0 counts as root)Read the final stage; a pass from a build-stage USER is no evidence
Root image, deployment sets the userEvery workload and command that runs the image, and its runAsUser or --userDismiss only if every one sets a non-root user; otherwise fix the image
runAsNonRoot: true with a root imageWhether runAsUser is also setWithout runAsUser the container fails to start; fix the image

The container image review guide covers the other runtime properties to follow from build to deployment target. A runtime override that only some callers set is the same pattern as a module default that only some environments override, covered in the guide to Terraform variable default findings.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading