Skip to content
BETAGuardrails are in beta. The library, the configuration format and the CLI command can still change.

Guardrails / iac

Dockerfiles run as a non-root user

iac/dockerfile-nonroot-user@v1

Every Dockerfile ends on a USER that is not root, so the container runs its process unprivileged.

Idiac/dockerfile-nonroot-user
Versionv1
Categoryiac
Default severityerror
Interpreterpython3
Timeout30 seconds
Violations tolerated0
Collectsdocker

Collectors

This guardrail gathers nothing itself. It depends on the collectors below, which the CLI runs once per build before any check, and reads what they found out of GUARDRAIL_FACTS. A collector that collects nothing skips this guardrail rather than failing it.

CollectorGathersInputs it is given
dockerEvery Dockerfile in the tree, parsed without a tool: the stages it builds, the image each one starts from and how that image is pinned, the user the image ends as, the ports it exposes, the paths it copies in and the names of the build arguments and environment variables it declares, enriched with the findings of a hadolint report when the build already left one behind.none

The inputs above are this guardrail's own, passed straight through. Configuring one in buildnote.json changes what is collected, and two guardrails configured the same way share the one collection.

Configuration

json
{
  "guardrails": {
      "failOn": "error",
      "comment": true,
      "checks": [
          {
              "use": "iac/dockerfile-nonroot-user@v1",
              "severity": "error",
              "with": {},
              "exemptions": []
          }
      ]
  }
}

How to fix

Create an unprivileged user in the image and switch to it before the entrypoint:

dockerfile
RUN addgroup -S app && adduser -S -G app app
USER app

The last USER in the file is what the container runs as, so a file that declares none runs as uid 0. A container that starts as root turns a bug in the application into a root-level problem on the host: a mounted path is writable, a capability is grantable, and an escape lands somewhere that matters.

Where the process has to bind a low port, publish it instead (docker run -p 80:8080) rather than staying root to bind 80.

More in iac

All 11 iac guardrails

Buildnote Limited
Registered in England and Wales, Reg: 16140412