What if a tiny permission change could stop hackers from breaking your system?
Why Capabilities and privilege control in Docker? - Purpose & Use Cases
Imagine you run many containers on your server, each needing different access levels. You try to give them all full access to avoid issues.
But this is like giving every employee the master key to the office, even if they only need the conference room.
Giving full privileges to all containers is risky and can lead to security breaches.
It's also hard to track who can do what, and mistakes can cause system crashes or data leaks.
Capabilities and privilege control let you give containers only the exact permissions they need.
This is like handing out keys only to the rooms employees need, keeping the rest locked and safe.
docker run --privileged mycontainer
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE mycontainer
This control makes your system safer and more stable by limiting what each container can do.
A web server container only needs permission to bind to network ports, not to change system files.
Using capabilities, you allow just that, reducing risk if the container is compromised.
Giving all privileges is risky and unnecessary.
Capabilities let you fine-tune container permissions.
This improves security and system stability.
Practice
--cap-drop option do when running a Docker container?Solution
Step 1: Understand Docker capabilities
Capabilities are Linux kernel permissions that control what a container can do on the host.Step 2: Role of
This option removes specific capabilities from the container, reducing its privileges.--cap-dropFinal Answer:
Removes specific Linux capabilities from the container -> Option AQuick Check:
--cap-dropmeans remove capabilities [OK]
- Confusing --cap-drop with --cap-add
- Thinking it controls CPU or memory
- Assuming it grants root access
NET_ADMIN capability to a Docker container?Solution
Step 1: Recall Docker capability syntax
The correct flag to add capabilities is--cap-add.Step 2: Match the correct command
docker run --cap-add NET_ADMIN myimage uses--cap-add NET_ADMIN, which is the exact syntax Docker expects.Final Answer:
docker run --cap-add NET_ADMIN myimage -> Option DQuick Check:
Use--cap-addto add capabilities [OK]
- Using incorrect flags like --add-cap
- Omitting the capability name
- Using --cap or --capability-add which are invalid
docker run --cap-drop ALL --cap-add CHOWN alpine id
Solution
Step 1: Analyze
This removes all Linux capabilities from the container, making it very restricted.--cap-drop ALLStep 2: Analyze
This adds back the CHOWN capability, allowing the container to change file ownership.--cap-add CHOWNFinal Answer:
The container has only the CHOWN capability enabled -> Option AQuick Check:
Drop all then add one = only that one capability [OK]
- Thinking --cap-add adds on top without drop effect
- Assuming container runs with full privileges
- Believing conflicting options cause failure
SYS_TIME capability but accidentally use this command:docker run --cap-drop SYS_TIME myimage
What is the problem and how to fix it?
Solution
Step 1: Understand the effect of
This removes the SYS_TIME capability, preventing the container from changing system time.--cap-drop SYS_TIMEStep 2: Correct the command to add the capability
To allow changing time, use--cap-add SYS_TIMEinstead of dropping it.Final Answer:
Problem: SYS_TIME is dropped, so container can't change time. Fix: Use--cap-add SYS_TIME-> Option BQuick Check:
Drop removes, add grants capabilities [OK]
- Confusing --cap-drop with --cap-add
- Assuming dropping caps grants them
- Thinking SYS_TIME is invalid capability
Solution
Step 1: Understand the goal
The container should only have network admin rights, no other privileges.Step 2: Evaluate options
docker run --cap-drop ALL --cap-add NET_ADMIN myimage drops all capabilities then adds only NET_ADMIN, perfectly limiting privileges.Step 3: Why others are wrong
--privilegedgrants full privileges,--cap-add ALLadds all capabilities (too much),--cap-drop NET_ADMINremoves NET_ADMIN (opposite of goal).Final Answer:
docker run --cap-drop ALL --cap-add NET_ADMIN myimage -> Option CQuick Check:
Drop all then add needed cap = minimal privileges [OK]
- Using --privileged which grants too many rights
- Adding all capabilities instead of limiting
- Dropping the needed capability by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
