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.
| Id | docker/nonroot-user |
| Version | v1 |
| Category | docker |
| Default severity | error |
| Interpreter | python3 |
| Timeout | 30 seconds |
| Violations tolerated | 0 |
| Collects | docker |
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.
| Collector | Gathers | Inputs it is given |
|---|---|---|
docker | Every 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
{
"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:
RUN addgroup -S app && adduser -S -G app app
USER appThe 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 aHEALTHCHECK, 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. NoARGorENVdeclared in a Dockerfile has a name that looks like a credential.