Video in progress
What is the risk of running the application as the root user inside a container?
If a vulnerability is found in the application, the attacker gets root inside the container, and if there is a container-escape vulnerability, reaching the host becomes easier. A Java service does not need root, a regular user is enough.
📖 Answer
By default, if no USER is specified, Docker runs the process as root (UID 0). Container isolation is not perfect: the kernel is shared, and with a badly mounted volume or an extra capability, root may be able to write to host files.
FROM eclipse-temurin:21-jre
RUN groupadd --system app && useradd --system --gid app app
WORKDIR /app
COPY --chown=app:app target/app.jar app.jar
USER app
ENTRYPOINT ["java", "-jar", "app.jar"]
In Kubernetes you can enforce this:
securityContext:
runAsNonRoot: true
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
If readOnlyRootFilesystem is on, Spring Boot's Tomcat may be unable to write temporary files to /tmp, so you mount an emptyDir volume there. Listening on a port below 1024 used to require root, which is why the app runs on 8080.
Sign up to mark studied questions and save your progress.