Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 61 of 8021. Container FundamentalsFree lesson

The Principle of Least Privilege

Apply the principle of least privilege by stripping unnecessary Linux capabilities from container processes to minimize the potential impact of a compromise.

Mastering Linux Capabilities for Container Security

In this lesson, you will learn how to shrink the "attack surface" of your containers by stripping away powerful Linux capabilities that your application doesn't actually need.

What are Linux Capabilities?

By default, Docker grants a container a specific set of powerful permissions, such as the ability to change file ownership, set system clocks, or even load kernel modules. While these are useful for a full-blown operating system, most web applications or microservices only need a tiny fraction of them to function.

Think of your container like an employee in a high-security building. If that employee only needs access to the breakroom and the lobby, you shouldn't give them a master key that opens the server room and the CEO’s office. If that employee’s ID badge is stolen, a master key could be catastrophic. In the world of containers, Linux Capabilities are those keys.

Stripping Unnecessary Privileges

The "Principle of Least Privilege" dictates that you should grant a process only the absolute minimum permissions required to perform its task—and nothing more. If your containerized app is compromised by a hacker, having fewer capabilities means the attacker is stuck in a "locked room" with no way to escalate their privileges or interact with the host kernel.

When you launch a container, you can drop capabilities using the --cap-drop flag. A best practice is to drop all of them and only add back what is strictly necessary:

# Drop all capabilities and only keep 'NET_BIND_SERVICE'
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE my-web-app

In this example, we remove everything from the container’s "keychain." Even if an attacker gains access to the container process, they won't be able to perform dangerous actions like changing the system clock or configuring network interfaces, because those capabilities have been stripped away. By explicitly limiting what your process can do, you contain the damage and protect the host system from potential exploits.

Key Takeaway: Always drop all Linux capabilities by default and only selectively add back the specific permissions your application requires to run.