A Dockerfile runs the app as root. How do you fix it and prove the fix?
IntermediateScenarioContainer and IaC Security
Answer
Situation read → Root in a container increases the impact of any breakout or volume misconfiguration.
What I'd do →
- Create a user and switch to it:
RUN addgroup -S app && adduser -S app -G app
USER 10001:10001
- Use a numeric UID so Kubernetes
runAsNonRootcan verify it. - Set
securityContext: runAsNonRoot: true, allowPrivilegeEscalation: false, capabilities.drop: [ALL]. - Enforce with Hadolint rule DL3002 and a Kyverno or PSA policy.
- Verify:
docker run --rm app:1.0 idshows non-zero uid.
Why → Numeric UID matters: kubelet cannot validate a named user.
Interview tip → Mention that ports below 1024 need NET_BIND_SERVICE or a higher port.
More on Container and IaC Security
A team asks whether to use `ubuntu`, `alpine` or a distroless base image. How do you advise?Beginner`trivy image` shows 300 vulnerabilities in your app image. How do you bring this down?BeginnerCheckov fails a Terraform PR because an S3 bucket allows public access. What do you change?BeginnerHow do you set up image scanning so nothing vulnerable reaches production?IntermediateAn image passed scanning three weeks ago and runs in production. A new critical CVE is published today. What happens next?IntermediateYou find `ENV DB_PASSWORD=...` and `ARG NPM_TOKEN` in a Dockerfile. What is wrong and how do you fix it?Beginner