Container Escape on Docker
How Container Escape Manifests in Docker
Container escape in Docker occurs when an attacker gains access to the host system from within a container. This represents one of the most severe security violations because it breaks the fundamental isolation boundary that containers are designed to provide.
The primary attack vectors in Docker environments include:
- Mounting the host root filesystem - When containers are run with
-v /:/hostor similar volume mounts, the container gains direct access to the host's file system, allowing modification of critical system files. - Privileged containers - Running containers with
--privilegedflag grants them access to all devices and disables most security mechanisms, effectively making them equivalent to running on the host directly. - Host networking - Using
--net=hostallows containers to share the host's network namespace, exposing host services and network interfaces. - Mounting Docker socket - Mounting
/var/run/docker.sockinto a container provides programmatic control over the Docker daemon, enabling the creation of new containers with elevated privileges. - Kernel exploit vulnerabilities - Container isolation relies on Linux kernel features like namespaces and cgroups. Vulnerabilities in these subsystems can be exploited to break out of container boundaries.
A common container escape pattern involves mounting the Docker socket and using it to launch a privileged container on the host:
Docker-Specific Detection
Detecting container escape vulnerabilities in Docker requires examining both runtime configurations and static analysis of Dockerfiles and compose files. middleBrick's Docker-specific scanning includes:
- Configuration analysis - Checking for dangerous flags like
--privileged, --net=host, and volume mounts that expose sensitive host paths. - Dockerfile inspection - Analyzing base images, user permissions, and installed packages that might introduce vulnerabilities.
- Runtime behavior monitoring - Detecting unusual process activities that suggest escape attempts, such as accessing
/proc/kcore or mounting /sys directories. - Network analysis - Identifying containers that share the host network namespace or expose privileged ports.
middleBrick scans Docker environments without requiring credentials or agents. Simply provide the Docker daemon URL or container endpoint:
middlebrick scan docker://unix:///var/run/docker.sock
The scanner checks for:
- Containers running with
--privileged flag - Volume mounts of sensitive host directories (
/, /etc, /proc, /sys) - Docker socket mounting (
/var/run/docker.sock) - Host networking usage
- Containers running as root without explicit user specification
- Outdated base images with known kernel vulnerabilities
For CI/CD integration, the middleBrick GitHub Action can automatically scan Docker images and fail builds if escape vulnerabilities are detected:
- name: middleBrick Docker Scan
uses: middlebrick/middlebrick-action@v1
with:
target: docker://myapp:latest
fail-on-severity: high
Docker-Specific Remediation
Securing Docker environments against container escape requires implementing defense-in-depth strategies and following Docker security best practices:
1. Principle of Least Privilege
Never run containers as root. Use the USER directive in Dockerfiles and the --user flag at runtime:
FROM alpine:3.18
RUN addgroup -g 1001 -S appgroup && \
adduser -u 1001 -S appuser -G appgroup
USER appuser
WORKDIR /app
docker run --user 1001:1001 myapp
2. Avoid Privileged Containers
Remove --privileged flags and use capabilities instead when specific privileges are needed:
# Instead of --privileged
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE myapp
3. Secure Volume Mounting
Be extremely selective about what you mount from the host. Use read-only mounts where possible:
# Bad: full host access
docker run -v /:/host myapp
# Better: specific read-only access
docker run -v /app/config:/app/config:ro myapp
4. Docker Socket Protection
Never mount the Docker socket into containers unless absolutely necessary. If required, use Docker's rootless mode:
# Instead of mounting the socket
docker run --userns=host myapp
5. Network Segmentation
Avoid --net=host and use user-defined networks with proper firewall rules:
docker network create --driver bridge --subnet 172.20.0.0/16 mynet
docker run --network mynet myapp
6. Runtime Protection
Use Docker's built-in security features like seccomp profiles and AppArmor/SELinux:
docker run --security-opt seccomp=seccomp.json myapp
middleBrick's Pro plan includes continuous monitoring that can detect when privileged containers are deployed or when dangerous configurations are introduced, alerting your team before vulnerabilities can be exploited.
Frequently Asked Questions
What's the difference between container escape and privilege escalation?
Privilege escalation occurs when a process gains higher privileges within the same container, while container escape involves breaking out of the container entirely to access the host system. Container escape is significantly more severe as it compromises the entire host and potentially all other containers running on it.How can I test if my Docker containers are vulnerable to escape attacks?
You can use middleBrick's free tier to scan your Docker endpoints. The scanner checks for common escape vectors like privileged containers, mounted Docker sockets, and dangerous volume mounts. For comprehensive testing, use the Pro plan which includes continuous monitoring and can detect escape attempts in real-time.