TL;DR
A root user inside a Docker container or LXC virtual machine can potentially break the security of your host system, but it’s not automatic. It depends on how those containers/VMs are configured and what privileges they have access to. Proper isolation is key.
Understanding the Risks
Docker and LXC provide isolation, but it’s not perfect. Think of them as separate rooms in a house – you want strong doors (isolation) between the rooms, but if someone gets enough tools or finds weaknesses, they might be able to get out.
Steps to Mitigate Risks
- Use Least Privilege: Don’t run containers/VMs as root unless absolutely necessary.
- Create a dedicated user inside the container/VM with only the permissions it needs.
- Specify this user when running the container/VM. For Docker, use the
--userflag:
docker run --user :
CAP_NET_ADMIN capability for network configuration but doesn’t need all of root’s powers.- List available capabilities:
man capabilities - Drop unnecessary capabilities when running containers/VMs using Docker’s
--cap-drop ALLand then add back only the required ones with--cap-add. Example:docker run --cap-drop ALL --cap-add NET_ADMIN
- Docker provides default seccomp profiles, but you can create custom ones for tighter security.
- Use Docker’s
--security-optflag to apply a profile:docker run --security-opt seccomp=
- Configure AppArmor or SELinux to define profiles for your containers/VMs. This is more complex but provides a strong layer of security.
- Use Docker volumes carefully and avoid mounting host directories unnecessarily. If you must mount a host directory, consider making it read-only:
docker run -v /host/path:/container/path:ro
- Namespaces: Isolate processes, network interfaces, user IDs, mount points, etc.
- Cgroups: Limit the amount of CPU, memory, and other resources a container/VM can use.
- Tools like Trivy or Clair can automate this process.
Specific Considerations
- Docker: Docker’s default settings provide a reasonable level of isolation, but it’s crucial to review and configure the options mentioned above for production environments.
- LXC: LXC is closer to full virtualization than Docker. While offering stronger isolation by default, misconfiguration can still lead to security breaches. Pay close attention to AppArmor/SELinux profiles and user privileges.