Junior
Why is it not recommended to run containers as the root user?
sobes.tech AI
Answer from AI
Running containers as the root user poses a security threat, as in case of container compromise, an attacker gains privileged access to the host system. This allows executing commands with high privileges, modifying system files, installing malicious software, and potentially gaining full control over the server or cluster.
It is recommended to use a non-privileged user inside the container, limiting its capabilities.
FROM ubuntu:latest
USER non_root_user # Create and use a new user
RUN groupadd -r appgroup && useradd -r -g appgroup non_root_user
RUN chown -R non_root_user:appgroup /app # Grant permissions to necessary directories
COPY . /app
WORKDIR /app
CMD ["./your_app"]
Reasons:
- Privilege escalation: A container process leak or vulnerability in the container engine can give an attacker root access on the host.
- Reducing attack surface: Running as a non-privileged user limits the attacker's capabilities if the container is compromised.
- Principle of least privilege: The application inside the container should have only the rights necessary to perform its functions.
Comparison:
| Attribute | Running as root | Running as a non-privileged user |
|---|---|---|
| Security | Low (risk of escalation) | High (restricted rights) |
| Access rights | Full privileges on the host | Limited inside the container |
| Complexity | Easier by default | Requires Dockerfile configuration |
| Risk to host | High | Low |