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.
More on Image design and Dockerfile decisions
A teammate's Node.js image is 1.2 GB. How do you approach shrinking it?BeginnerYou need to run `apt-get install` in a Dockerfile. How do you write it so the layer stays small and cache-safe?BeginnerWhen would you pick `COPY` over `ADD`?BeginnerA service must run a startup script and then the app. Should you use `CMD`, `ENTRYPOINT`, or both?BeginnerYour Docker build re-installs all Python dependencies on every code change. Fix it.BeginnerProduct asks for a container that runs both nginx and a Python app. What do you say?Beginner