Home DockerDocker Containers as Non-Root: Complete Secure Guide

Docker Containers as Non-Root: Complete Secure Guide

by Knowledge Base Linux
Docker non-root containers secure setup with dedicated user

Running Docker non-root containers is one of the simplest security wins in container work, because by default a container process runs as root, and root inside a container is closer to root on the host than most people realise. A container breakout, a writable Docker socket mount, or a misconfigured volume can turn that in-container root into real host access. Dropping to a dedicated non-root user does not fix every container risk, but it removes the easiest and most damaging path: an attacker who escapes the container, or abuses a mounted path, no longer starts with full privileges. This guide shows how to build images that run as a non-root user, how to enforce it at run time, and how to fix the volume permission problems that make teams give up and go back to root.

It helps to be precise about what non-root actually protects. Inside a container, user namespaces are not enabled by default, so UID 0 in the container is UID 0 on the host for files the container can reach through a bind mount or a shared volume. A process running as UID 10001 cannot write to host files owned by root, cannot read a mounted Docker socket with root-only permissions, and cannot perform privileged operations even if it finds a way out of the container filesystem. That is why the rule is worth enforcing even for internal services: the blast radius of a compromised application shrinks from everything the host can do to only what that one user can do. For a broader look at container isolation, our guide to Docker rootless mode on Ubuntu 24.04 covers running the whole daemon without root, which pairs well with non-root containers.

Docker non-root containers least privilege user and shield

Prerequisites

You need Docker Engine installed on a Linux host and a small application to containerise; the examples use a simple web server, but the pattern is identical for APIs, workers, and background jobs. You should be comfortable building an image with docker build and running it with docker run. No orchestrator is required: the same USER instruction works in Kubernetes, Compose, and plain Docker. Keep in mind that switching an existing image to a non-root user is a breaking change for anything that writes to files owned by root inside the image, so test the change in a staging environment before deploying it to production.

Docker Non-Root Containers: Build Images with a Dedicated User

The cleanest approach is to create a dedicated user in the Dockerfile and switch to it with the USER instruction as the last step before the entrypoint. Create the user with a fixed UID and GID so the same identity is predictable across rebuilds, create only the directories the application needs, and set ownership on those directories before dropping privileges. Order matters: everything that needs root, such as installing packages and creating directories, happens before USER, and nothing after USER should need root.

FROM python:3.12-slim
RUN groupadd -r appgroup --gid 10001 && \
    useradd -r -g appgroup --uid 10001 appuser
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
RUN mkdir -p /app/data && chown -R appuser:appgroup /app
USER appuser
CMD ["python", "app.py"]

Use a numeric UID in the USER instruction in production images, such as USER 10001, rather than a name. Names resolve through the image passwd file, which is fine, but a numeric UID keeps behaviour identical if the image is later used as a base image or inspected by a security scanner that checks for a non-zero UID. Also avoid binding to privileged ports below 1024: a non-root process cannot listen on port 80, so have the application listen on 8080 and map it to 80 on the host with -p 80:8080. The official Docker documentation on the Dockerfile USER instruction describes how USER affects RUN, CMD, and ENTRYPOINT, and it is worth reading before you restructure a complex image.

Docker Non-Root Containers: Enforce It at Run Time

Even an image that defaults to root can be run as a non-root user with the –user flag, which is useful for third-party images you cannot rebuild. Pass a UID and GID, and Docker runs the container process as that identity regardless of the image default. Combine it with a read-only root filesystem and a writable tmpfs for the directories the application genuinely needs to write to, which stops an attacker from persisting changes inside the container even if they gain code execution.

docker run -d --name web \
  --user 10001:10001 \
  --read-only --tmpfs /tmp \
  -p 8080:8080 myapp:latest

In Compose, the same controls are user, read_only, and tmpfs. A Compose file that declares these for every service makes the non-root policy visible in version control instead of relying on each developer remembering a run flag. For services that need to write to a data directory, prefer a named volume over a bind mount: Docker initialises a new named volume with the ownership of the directory in the image, while a bind mount keeps the host directory ownership, which is the source of most permission errors.

services:
  web:
    image: myapp:latest
    user: "10001:10001"
    read_only: true
    tmpfs:
      - /tmp
    volumes:
      - appdata:/app/data
volumes:
  appdata:

Never mount the Docker socket into a non-root container as a shortcut around permissions. The socket grants control over the daemon, and a process that can talk to it can start a new privileged container, which defeats the entire non-root policy. If a service genuinely needs Docker access, give it a dedicated, narrowly scoped API or run it outside the container, and see our guide to Docker Secrets for the parallel lesson on keeping credentials out of containers.

Troubleshooting

Permission denied on a bind mount is the most common failure, and it has a simple cause: the host directory is owned by root or by your login user, while the container runs as UID 10001. Fix it on the host by changing ownership to the container UID, with chown -R 10001:10001 on the mounted directory, or switch to a named volume and let Docker handle ownership. Do not fix it by running the container as root again, and do not fix it by making the host directory world-writable, because both undo the security gain you just made.

When an application fails with permission denied while writing to a path inside the image, check which step created that path: anything created after USER in the Dockerfile is owned by the new user, but anything copied before USER is owned by root unless you chown it. The COPY instruction supports –chown, so COPY –chown=appuser:appgroup app.py . is the clean fix for files the application must write to. When a container exits immediately with a port error, check whether the application tries to bind below port 1024; move it to 8080 or higher. And when a security scanner still reports the image as running as root, check for a USER instruction in the final stage of a multi-stage build: an early stage can run as root, but only the final stage determines the runtime user.

Conclusion

Docker non-root containers are a habit, not a single setting: build images with a dedicated user and a numeric UID, enforce the user at run time for images you do not control, use named volumes for writable data, and keep the filesystem read-only where you can. The first image you convert will surface a permission problem or two, and fixing those properly, with ownership and named volumes, is the work that makes the policy stick. After that, non-root becomes the default, and root containers become the exception that needs a written reason.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.