User Check

Difficulty: Beginner

Question

How do you check which user a process runs as inside a container without opening an interactive shell, and why does it matter?

Answer

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.

Checking without any tools inside the image

Minimal and distroless images often have no id, whoami or shell. Two checks work from the host:

  • Configured user: 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).
  • Actual running processes: 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.

Why interviewers care: root in a container

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:

  • Official images like node ship a non-root node user, though you still have to switch to it with USER node.
  • Kubernetes Pod Security Standards (restricted profile) require runAsNonRoot: true.
  • Security scanners flag images whose last USER is root.

Ways to run as non-root

ApproachHow
In the imageRUN adduser -D -u 10001 app then USER 10001 in the Dockerfile
At runtimedocker run --user 10001:10001 ...
User namespace remappingDaemon option userns-remap maps container root to an unprivileged host UID
Rootless DockerThe 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.

Common gotchas

  • Permission denied after switching user. Files copied in earlier stages are owned by root. Use COPY --chown=10001:10001.
  • Ports below 1024. A non-root process cannot bind port 80 by default; listen on 8080 and map it.
  • Debugging as root. docker exec -u 0 <container> sh gets a root shell for troubleshooting without changing the container's normal user.
Checking the container user, from easiest to most robust. Start with id inside the container and fall back to host-side checks for minimal images.
  1. docker exec <id> id: Prints UID, GID and groups of the exec user.
  2. docker inspect Config.User: Shows the configured user; empty means root.
  3. docker top <id> -eo pid,user,args: Shows the real user of every running process.
  4. Fix in Dockerfile if root: Add a numeric USER and COPY --chown.

Code examples

Three ways to check the 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.

Dockerfile that runs as a numeric non-root 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.

Key points

  • docker exec <id> id is more reliable than whoami, which fails for UIDs not in /etc/passwd
  • docker inspect -f '{{.Config.User}}' shows the configured user; empty means root
  • docker top shows the real user of every process from the host side
  • Root in a container is UID 0 on the host unless user namespaces or rootless mode are used
  • Use a numeric USER and COPY --chown to run as non-root cleanly

Common follow-up questions

  • Why is running a container as root a security risk if containers are isolated?
  • What is user namespace remapping, and what does rootless Docker change?
  • How do you make a non-root container listen on port 80?
  • How does Kubernetes enforce non-root containers?

Interview tips

  • Prefer id over whoami and explain why; small precision like this stands out
  • Connect the check to security practice: non-root users, runAsNonRoot and least privilege

Concepts covered

Container user (UID/GID), docker exec id, USER instruction, Running as non-root, docker top

More Docker & Containers questions