Skip to content

Guardrails / docker

Dockerfiles run as a non-root user ​

docker/nonroot-user@v1

Every Dockerfile ends with a USER that is not root, so the container runs its process without privileges.

Iddocker/nonroot-user
Versionv1
Categorydocker
Default severityerror
Interpreterpython3
Timeout30 seconds
Violations tolerated0
Collectsdocker

Collectors ​

This guardrail doesn't gather anything itself. It relies on the collectors below, which the CLI runs once per build before any check, and reads what they found from GUARDRAIL_FACTS. If a collector collects nothing, this guardrail is skipped, not failed.

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, plus the findings of a hadolint report when the build already left one behind.none

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

Configuration ​

json
{
  "guardrails": {
      "failOn": "error",
      "checks": [
          {
              "use": "docker/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 container runs as the last USER in the file, so a Dockerfile that declares none runs as uid 0. When a container starts as root, a bug in the application becomes a root-level problem on the host: mounted paths are writable, capabilities can be granted, and an escape lands somewhere that matters.

If the process needs a low port such as 80, publish a high port to it (docker run -p 80:8080) instead of staying root to bind 80.

More in docker ​

  • docker/base-pinned. Every image a Dockerfile builds from is pinned to a digest, so rebuilding the same commit starts from the same bytes.
  • docker/healthcheck. Every Dockerfile declares a HEALTHCHECK, so the orchestrator can tell a container that is running from one that is actually working.
  • docker/lint-clean. Every finding the Dockerfile linter reported is below the severity your team gates on.
  • docker/no-build-secrets. No ARG or ENV declared in a Dockerfile has a name that looks like a credential.

All 5 docker guardrails

Buildnote Limited
Registered in England and Wales, Reg: 16140412