Sub topic 3
Read-Only Root Filesystems and Resource Limits
Assume you know how to build and run Docker images, use Linux commands, and work with users, permissions, and processes. This chapter focuses on controls that reduce the damage a compromised container can cause.
Restricting Runtime Behavior
A read-only root filesystem prevents a container process from changing files in the image. Give specific paths writable with temporary filesystems or mounted volumes. Resource limits reduce denial-of-service risk by constraining memory, CPU, and process creation.
docker run -d --name web \
--user 10001:10001 \
--cap-drop ALL \
--security-opt no-new-privileges \
--read-only --tmpfs /tmp \
--memory 256m --cpus 0.5 --pids-limit 100 \
myapp:1.0no-new-privileges prevents a process from gaining privileges through set-user-ID binaries. Test the application first because some programs need writable directories or specific Linux capabilities.
Syscall and Mandatory Access Control Profiles
Seccomp filters Linux system calls. Docker's default profile blocks many dangerous calls, so keep it unless the application has a documented need for additional calls. AppArmor or SELinux adds mandatory access control. These systems restrict file, capability, and process actions even when normal Unix permissions allow them.
Use a custom profile only after testing. A profile that is too strict can break harmless library or runtime behavior.
Protecting the Daemon and Network
Access to /var/run/docker.sock is effectively host-level control. Do not mount the socket into ordinary containers, and restrict membership in the docker group. Prefer rootless Docker or user-namespace remapping when operationally practical. These modes map container root to an unprivileged host identity.
Publish only required ports. Bind administrative services to localhost or a private interface, and use firewall rules or network policies to restrict container-to-container traffic. Avoid --network host unless the application requires it.
Verifying and Monitoring Images
Pin important base images by digest, scan them for vulnerabilities, and review whether a finding is reachable and has a usable fix. Generate an SBOM, which lists software components in the image. Require signed images and provenance attestations in the deployment pipeline so the registry content and build origin can be verified.
Audit the host against the CIS Docker Benchmark. Also monitor container starts, privilege changes, unexpected network connections, file writes, and resource spikes. Logs and alerts should identify the container, image digest, user, and initiating process.
Exercise
A container needs to write temporary files and is killed after a memory spike. Which controls should you check?
Show answer
Mount a writable tmpfs such as /tmp, then set and observe a suitable --memory limit. Check application logs and container metrics before raising the limit.