Difficulty: Beginner
How do you check which user a process runs as inside a container without opening an interactive shell, and why does it matter?
Run docker exec <container> id, which prints the UID, GID and groups of the default exec user, for example uid=1000(node) gid=1000(node) groups=1000(node). docker exec <container> whoami also works but only prints a name, and fails when the UID has no entry in the image's /etc/passwd, so id is the more reliable choice.
Minimal and distroless images often have no id, whoami or shell. Two checks work from the host:
docker inspect -f '{{.Config.User}}' <container> shows the user set by USER in the Dockerfile or by --user. An empty result means the default, which is root (UID 0).docker top <container> -eo pid,user,args runs ps on the host for that container's processes. This shows what is really running, including child processes that switched user, such as nginx workers running as nginx while the master runs as root.Note that docker exec runs as the container's configured user by default, which is not necessarily the user of PID 1 if the entrypoint drops privileges with gosu or su-exec. To see PID 1 specifically, read /proc/1/status or use docker top.
By default, root inside a container is the same UID 0 as root on the host, just restricted by namespaces, capabilities and seccomp. If an attacker escapes through a kernel or runtime bug, or if a host directory is bind-mounted, running as root makes the damage much worse. That is why:
node ship a non-root node user, though you still have to switch to it with USER node.runAsNonRoot: true.USER is root.| Approach | How |
|---|---|
| In the image | RUN adduser -D -u 10001 app then USER 10001 in the Dockerfile |
| At runtime | docker run --user 10001:10001 ... |
| User namespace remapping | Daemon option userns-remap maps container root to an unprivileged host UID |
| Rootless Docker | The whole daemon runs as an unprivileged user |
Using a numeric UID in USER lets Kubernetes verify runAsNonRoot; with a name only, the kubelet cannot confirm the UID is non-zero and refuses to start the pod.
COPY --chown=10001:10001.docker exec -u 0 <container> sh gets a root shell for troubleshooting without changing the container's normal user.# 1. Run id inside the container (no interactive shell needed)
docker exec api id
# 2. Configured user from the container config (works for distroless)
docker inspect -f 'user="{{.Config.User}}"' api
# 3. Real processes as seen from the host
docker top api -eo pid,user,args
# Root shell for debugging only
docker exec -u 0 -it api sh
docker top runs ps on the host, so the USER column shows the host's name for that UID, or the raw number if the host has no such user.
FROM node:22-alpine
WORKDIR /app
COPY --chown=node:node package*.json ./
RUN npm ci --omit=dev
COPY --chown=node:node . .
# node user in the official image has UID 1000
USER 1000
EXPOSE 8080
CMD ["node", "dist/server.js"]
The numeric USER lets Kubernetes enforce runAsNonRoot, and --chown makes sure the non-root user can read its own files.
Container user (UID/GID), docker exec id, USER instruction, Running as non-root, docker top