Interview Prep/Docker/Image design and Dockerfile decisions

How do you run a container as a non-root user in a Dockerfile properly?

BeginnerScenarioImage design and Dockerfile decisions
Answer

Situation read → Default is root; compromise of the app gives root inside the container.

What I'd do →

RUN groupadd -r app --gid=10001 && useradd -r -g app --uid=10001 app
WORKDIR /app
COPY --chown=app:app . .
USER 10001:10001

Use a numeric UID so Kubernetes runAsNonRoot can verify it.

Why → Limits blast radius and works with read-only root filesystems.

Interview tip → Ports below 1024 need CAP_NET_BIND_SERVICE or sysctl; use 8080 instead.

All Docker questions Roadmap: Docker & containers

More on Image design and Dockerfile decisions