Skip to content
Blog

Running Containers as Non-Root Users for Secure Execution

A practical guide to securing Docker containers by overriding root execution using -u and --user flags, configuring non-root Dockerfile users, rootless Docker, and resolving volume permission issues.

Published on • November 17, 2026

AI Assistant

By default, Docker containers run their internal main process as the root superuser (UID 0) unless specified otherwise. If an attacker achieves container escape through an unpatched kernel vulnerability or runtime exploit, running as root grants elevated privileges over the underlying host system.

Enforcing non-root execution is one of the most critical security hardening steps in container architecture.


1. Overriding Container User with -u / --user

You can enforce a specific non-root User ID (UID) and Group ID (GID) at container launch using the -u or --user flag:

# Run container as UID 1000 and GID 1000
docker run -d --name secure-app \
  -u 1000:1000 \
  alpine tail -f /dev/null

Specifying Users by Name or ID

The --user flag accepts multiple formats:

# By numerical UID
docker run -u 1000 my-image

# By numerical UID and GID
docker run -u 1000:1000 my-image

# By username defined inside the image's /etc/passwd
docker run -u node node:20-alpine whoami

Note: If you pass a username (e.g., -u node), that user must already exist in the container image’s /etc/passwd file. Passing numerical UIDs (-u 1000:1000) works even if the UID does not exist in /etc/passwd.


2. Hardening Images inside the Dockerfile

Rather than relying on operators to pass -u manually at runtime, container images should define unprivileged users directly inside the Dockerfile using the USER instruction:

FROM node:20-alpine

# Create a non-root group and user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# Set working directory ownership
WORKDIR /app
COPY --chown=appuser:appgroup . /app

# Switch to non-root user
USER appuser

EXPOSE 3000
CMD ["node", "server.js"]

Subsequent build stages and container runs will default to appuser (non-root), adhering to the principle of least privilege.


3. Resolving Permission Denied Errors with Bind Mounts

Running containers as a non-root UID frequently leads to Permission Denied errors when mounting host directories via bind mounts (-v /host/path:/container/path).

The Root Cause

When Docker creates a bind mount, files created on the host belong to the host owner UID. If host directory permissions restrict access to UID 1000 and your container runs as UID 1001 or nobody, the container process cannot write to the mounted volume.

Remediation Strategies:

  1. Match Host UID/GID at Runtime: Match the host user’s UID and GID dynamically when launching the container:

    docker run -d --name app \
      -u $(id -u):$(id -g) \
      -v $(pwd)/data:/app/data \
      my-app:latest
  2. Pre-Create Host Directories with Proper Permissions: Ensure host directory ownership matches the container’s designated UID prior to running the container:

    mkdir -p ./data
    chown -R 1000:1000 ./data

4. Rootless Docker Engine Execution

For maximum host security, organization environments can adopt Rootless Docker. Rootless mode runs both the Docker daemon (dockerd) and containers inside a user namespace without root privileges on the host system.

In Rootless Docker:

  • Root (UID 0) inside the container maps to an unprivileged host UID on the outside.
  • An attacker breaking out of a container gains no root permissions on the host system.

5. Summary and Best Practices

Takeaway: Protect hosts against container breakout exploits by running processes under unprivileged UIDs using -u UID:GID, embedding non-root USER instructions in Dockerfiles, matching host file ownership for bind mounts, and deploying Rootless Docker wherever possible.


Sources