Runtime Container Security
Implement best practices for securing containers at runtime, including user privileges and resource limits.
Runtime Container Security is a free Docker & DevOps Fundamentals lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & DevOps Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Runtime Security Essentials
Welcome to Runtime Container Security! Building secure images is crucial, but what happens once your container is running?
This lesson focuses on best practices to protect your applications while they are active, limiting potential damage from vulnerabilities or attacks.

Principle of Least Privilege
A fundamental security concept is the Principle of Least Privilege. It means giving an entity (like a container or user) only the permissions absolutely necessary to perform its function, and no more.
Applying this reduces the attack surface and limits the impact if a container is compromised.
Avoid Running as Root
By default, processes inside a Docker container run as the root user, which has full administrative privileges within the container.
- Risk: If an attacker gains control of a root-privileged container, they could potentially exploit vulnerabilities in the Docker daemon or kernel to gain root access on the host system.
- Best Practice: Always run your container processes as a non-root user.
Run as a Non-Root User
You can specify a user (by name or UID) for your container process using the --user flag with docker run. Here, we run the id command in an Alpine container as user 1000.
If user 1000 doesn't exist, Docker will still use that UID.
docker run --rm -it --user 1000 alpine idUnderstanding Linux Capabilities
Traditional Linux systems have an 'all or nothing' root user. Linux Capabilities break down the powerful root privileges into smaller, distinct units.
This allows a process to have only the specific root-like powers it needs (e.g., binding to low ports, raw network access) without having full root.
Drop Unneeded Capabilities
Docker containers by default run with a large set of capabilities. You can drop unnecessary capabilities using --cap-drop to further restrict what a container can do.
Here, we drop the NET_RAW capability. The ping command, which requires NET_RAW, will then fail, demonstrating the restriction.
docker run --rm -it --cap-drop=NET_RAW alpine ping -c 1 localhost || echo "Ping failed: NET_RAW capability dropped!"Control Container Resources
Containers share the host's kernel and resources. Uncontrolled resource usage by one container can lead to a Denial of Service (DoS) for other containers or even the host itself.
- CPU Limits: Prevent a container from monopolizing CPU cycles.
- Memory Limits: Stop a container from consuming all available RAM, preventing system instability.
Implement Resource Limits
You can set limits on CPU and memory directly with docker run. This example limits memory to 128MB and CPU usage to 0.5 (half of one CPU core).
This ensures your container behaves well and doesn't starve other processes.
docker run --rm -it --memory="128m" --cpus="0.5" alpine sh -c "echo 'Container running with limited resources.' && free -h"Read-Only Filesystems
Many applications don't need to write to their root filesystem after startup. By making the filesystem read-only, you gain significant security benefits:
- Prevents Tampering: An attacker cannot modify existing files or write new malicious files.
- Limits Persistence: Any changes made are ephemeral and lost upon container restart.
- Enforces Immutability: Promotes a design where containers are disposable and configuration is external.
Deploy Read-Only Containers
Use the --read-only flag when running a container. Any attempt to write to the container's filesystem (outside of explicitly mounted volumes) will fail.
Try creating a file in this read-only container:
docker run --rm -it --read-only alpine sh -c "touch /test.txt || echo 'Error: Cannot write to read-only filesystem!'"Runtime Security Check
Which of the following are good practices for securing containers at runtime?
Runtime Security Recap
Great job! You've learned how to enhance container security while they are running:
- Least Privilege: Grant only necessary permissions.
- Non-Root Users: Avoid running processes as
root. - Capabilities: Drop unneeded Linux capabilities.
- Resource Limits: Control CPU and memory usage.
- Read-Only: Make filesystems immutable to prevent writes.
These practices significantly reduce the attack surface and impact of potential compromises. Keep practicing!
Frequently asked questions
Is the “Runtime Container Security” lesson free?
Yes — the full text of “Runtime Container Security” is free to read here on the web, and the Docker & DevOps Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & DevOps Fundamentals course, upgrade to CoddyKit PRO.
What will I learn in “Runtime Container Security”?
Implement best practices for securing containers at runtime, including user privileges and resource limits. You practise Docker & DevOps Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Docker & DevOps Fundamentals?
No prior experience is required. Docker & DevOps Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Runtime Container Security” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Docker & DevOps Fundamentals lesson?
Yes. Every Docker & DevOps Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.