Containers must not run their final process as root because running as root increases the impact of a compromise and can enable privilege escalation or container-to-host attacks.
This rule checks the Dockerfile USER instruction and flags Dockerfiles whose last USER is set to root. The final USER must be a non-root username or UID.
If root is required for build-time actions, perform those steps earlier (for example, in a build stage), then create a non-root user and set USER to that account before the final CMD/ENTRYPOINT.
Secure example that switches to a non-root user before runtime:
FROMnode:18ASbuildRUN npm ci && npm run buildFROMnode:18-slimWORKDIR/appCOPY --from=build /app/dist .RUN addgroup --system app && adduser --system --ingroup app appUSERappCMD["node","server.js"]
Compliant Code Examples
FROMalpine:2.6USERrootRUN npm installUSERguest
FROMgolang:1.16ASbuilderWORKDIR/go/src/github.com/foo/href-counter/RUN go get -d -v golang.org/x/net/html COPY app.go ./RUNCGO_ENABLED=0GOOS=linux go build -a -installsuffix cgo -o app .USERrootFROMalpine:latestRUN apk --no-cache add ca-certificatesWORKDIR/root/COPY --from=builder /go/src/github.com/foo/href-counter/app ./CMD["./app"]RUN useradd -ms /bin/bash patrickUSERpatrick
Non-Compliant Code Examples
FROMalpine:2.6USERrootRUN npm install
1
2
rulesets:- Dockerfile # Rules to enforce .
パーソナライズされたデモをリクエスト
Datadog を始める
Ask AI
AI-generated responses may be inaccurate. Verify important info.