What user does a container process run as if you don't specify one, and how do you make it run as a non-root user?
By default the container's main process runs as root (UID 0). Define a non-root user with a USER instruction in the Dockerfile, or pass --user <uid>:<gid> at run time.
If no user is set, whoami in a fresh container returns root. That is a standing invitation: combined with a bind mount or the Docker socket, container-root easily becomes host-root, and many of the chapter 3 attacks depend on it.
Two ways to drop to a non-root user:
-
In the Dockerfile — create a locked-down system account and switch to it:
FROM alpine RUN mkdir /userdata RUN addgroup -S islab && adduser -S -s /sbin/nologin -G islab islab RUN chown -R islab:islab /userdata USER islabThe account has no password, no home, and
/sbin/nologinas its shell, so it can't be logged into interactively; it only owns the data it needs. -
At run time —
docker container run --user 1000:1000 …(use an ID ≥ 1000, or the user's name for images that define one such asnginx).
Running non-root means that even if the app is exploited, the attacker is confined to that unprivileged user: they can't read /etc/shadow (permissions 0640, shadow group only) and can't trivially escalate. It is one of the single most effective Docker hardening steps, and underpins user namespaces.
Go deeper:
Docker Docs: Docker Engine security — why non-root containers matter and how to set a user.