ContainersDocker · Kubernetes
Run containers as a non-root user
The default user within a container is root. A USER instruction in the Dockerfile and a few securityContext fields in the pod spec change that.

Why root in a container matters
Docker's run reference states the starting point: inside a container the default user is root (UID 0), and a USER instruction in the Dockerfile changes that default.
The Kubernetes documentation states the risk. With no user namespace, a container running as root has root privileges on the node in the case of a container breakout.
Both projects give the same advice. Docker's best-practices page says to use USER to change to a non-root user if a service can run without privileges. The Kubernetes application security checklist says to set runAsNonRoot: true and to configure the container to execute as a less privileged user.
A non-root user in the Dockerfile
This Dockerfile is from Docker's Compose documentation:
# Run as a non-privileged user
FROM node:18
RUN useradd -ms /bin/sh -u 1001 app
USER app
# Install dependencies
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
# Copy source files into application directory
COPY --chown=app:app . /appuseradd creates the user app, and -u 1001 gives it that numeric user ID. USER app then sets the user for the RUN instructions that follow in the stage, and for the ENTRYPOINT and CMD commands at run time.
The UID 1001 is the documentation's choice. The best-practices page explains why to state one: users and groups in an image get the next UID and GID regardless of image rebuilds, so assign explicit ones if it is critical.
USER also has a numeric form, USER <UID>[:<GID>]. The Dockerfile reference warns that when the user has no primary group, the image runs with the root group.
The numeric form matters in Kubernetes. With runAsNonRoot: true and no runAsUser, the kubelet's source code returns the error container has runAsNonRoot and image has non-numeric user when the image's user is a name. The message adds that it cannot verify the user is non-root.
File ownership with COPY --chown
Without --chown, COPY creates files with a UID and GID of 0. The last line of the example uses --chown=app:app, so the copied files are owned by the user the container runs as. The Compose page gives its reason: For Compose Watch, the container's USER needs write access to the target path.
The flag accepts names and numeric IDs. Names are resolved with /etc/passwd and /etc/group in the container's root filesystem, and the build fails if they are missing. Numeric IDs need no lookup, and --link requires them.
For volumes in Kubernetes, the pod-level fsGroup field sets the group. In the Kubernetes example, a volume in a pod with fsGroup: 2000 has group ID 2000, and so does a file the process creates in it.
Ports below 1024
NET_BIND_SERVICE is the capability to bind a socket to privileged ports, which Docker's run reference defines as port numbers less than 1024. It is in the list of capabilities Docker allows by default. Two runtimes also document a kernel setting:
- The Docker Engine 20.10 release notes list default sysctls that allow privileged ports with no capabilities.
- The containerd CRI configuration has an option,
enable_unprivileged_ports, that setsnet.ipv4.ip_unprivileged_port_start=0for all containers that are not using the host network. Before containerd v2.0 it defaulted tofalse.
Kubernetes lists net.ipv4.ip_unprivileged_port_start among its safe sysctls since version 1.22, and a pod sets namespaced sysctls through its securityContext.
The port a container listens on does not have to be the port clients use. A Kubernetes Service can map any incoming port to a targetPort.
runAsNonRoot in Kubernetes
This Pod is from the Kubernetes documentation:
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo-2
spec:
containers:
- name: sec-ctx-demo-2
image: gcr.io/google-samples/node-hello:1.0
securityContext:
allowPrivilegeEscalation: false
runAsNonRoot: true
runAsUser: 1000
capabilities:
drop:
- ALL
seccompProfile:
type: RuntimeDefaultThe names, the image and the UID 1000 are the documentation's. Use your own. What the fields do:
runAsNonRoot: truemakes the kubelet validate the image at run time. If the image would run as UID 0, the kubelet fails to start the container.runAsUseris the UID that runs the entrypoint of the container process. It defaults to the user specified in the image metadata.allowPrivilegeEscalationdecides whether a process may end up with more privileges than the process that started it. Running a setuid binary is one way that happens. It defaults totrue.runAsGroupis not in this example. If it is omitted, the primary group ID of the containers is root (0).
The Restricted Pod Security Standard requires runAsNonRoot: true and does not allow runAsUser: 0.
Rootless mode and user namespaces
A user namespace maps users in the container to different users on the host. Docker's rootless mode runs the daemon and the containers inside one, both with no root privileges. With userns-remap, the daemon itself still runs as root. In Kubernetes, a pod opts in with hostUsers: false. The User Namespaces page marks the feature stable since v1.36 and requires containerd 2.0 or CRI-O 1.25, and at least Linux 6.3. The page on Linux kernel security constraints still calls it in early stages of development. The two pages disagree.
How to check a container
The Kubernetes task page checks the user from inside the container. security-context-demo is the name of the pod in its example:
kubectl exec -it security-context-demo -- shIn that shell, run id. The documented output for the example pod is uid=1000 gid=3000 groups=2000,3000,4000, where 1000 is its runAsUser and 3000 its runAsGroup.
With Docker, docker run -u overrides the USER instruction when a container starts.
Sources
Want a second pair of eyes on yours? We do this work for a living.
More guides

securityContext: the settings every pod should carry
Security · 4 min read
Six settings, where each one goes, and a Deployment you can copy.

Pod Security Standards: baseline, restricted and how to enforce them
Security · 4 min read
Three levels, six namespace labels and a rollout that warns before it blocks.

Choosing a container registry: ECR, ACR, Artifact Registry, Harbor or JFrog
Containers · 4 min read
Nine things to compare, then ECR, ACR, Artifact Registry, Harbor and JFrog Artifactory side by side.