Run hardened images on Kubernetes.

A Dockerfile hardened by Rivex ends with a numeric USER 10001:10001, so the image starts on clusters that require non-root containers without any change to the pod.

Why the user is a number.

A pod with runAsNonRoot: true and no runAsUser makes the kubelet read the image's USER and prove it is not root. It can prove that only for a number. A user name might be root, so the kubelet refuses to start the container, and the pod stays in CreateContainerConfigError:

container has runAsNonRoot and image has non-numeric user (nonroot), cannot verify user is non-root

The Pod Security Standard called restricted, and policy tools such as Kyverno and Gatekeeper, require runAsNonRoot: true. Docker, Compose, ECS and Cloud Run don't make this check.

What Rivex adds.

When the final stage has no USER instruction and creates no user itself, Rivex appends two lines. A RUN instruction makes sure a user with UID 10001 and a group with GID 10001 exist, and creates them as nonroot when they don't. Then USER 10001:10001 switches to them. UID 10001 is above 10000, so it never matches an account on the host.

Two cases need no action from you. If the base image already has a user with UID 10001, or a nonroot user at 10001:10001, Rivex uses that account.

When the build stops with rivex.

The RUN instruction never switches to a different account silently. It fails the build with a line that starts with rivex: when:

  • the base image has a nonroot user or group with other IDs, as the distroless and Chainguard images do with 65532.
  • the base image does not run as root, so it can't create users.
  • the base image has no shell or no user tools, as the minimal Red Hat images do.
  • Alpine already has a group with GID 10001 under another name while UID 10001 is free.

In each case, set USER yourself to the numeric UID and GID of an account that exists in the image, for example USER 65532:65532.

When your Dockerfile ends with a user name.

If your Dockerfile already ends with a user name, such as USER app, Rivex does not change it. It reports a medium finding, because that image fails on a cluster with runAsNonRoot: true. Replace the name with USER <uid>:<gid>, the numeric UID and GID of the same user, or set runAsUser in the pod.

A pod that works.

This Deployment passes the restricted standard with a Rivex-hardened image. Replace the image with your own:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      securityContext:
        runAsNonRoot: true
        fsGroup: 10001
        seccompProfile:
          type: RuntimeDefault
      containers:
        - name: app
          image: registry.example.com/team/app:1.0
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop: ["ALL"]

Setting runAsUser and runAsGroup is optional, but if you set them they must be 10001, the IDs of the user in the image.

Volumes and OpenShift.

fsGroup: 10001 makes Kubernetes give the pod's user write access to mounted volumes that support it. Without it, a volume owned by root is read-only for the container.

OpenShift assigns each project its own UID and ignores the one in the image. Leave runAsUser and runAsGroup out, as the example does, and the pod runs there unchanged.

OpenShift runs the assigned UID with group 0, so the directories the app writes to need group-0 write access, for example chgrp -R 0 /data && chmod -R g=u /data in the Dockerfile (use your own path in place of /data).