Capabilities and privilege control in Docker - Time & Space Complexity
We want to understand how the time it takes to start a Docker container changes when we add or remove capabilities or privilege controls.
How does changing these security settings affect the container startup time?
Analyze the time complexity of the following Docker run command.
docker run --rm \
--cap-drop=ALL \
--cap-add=NET_ADMIN \
--security-opt no-new-privileges \
alpine \
ping -c 1 8.8.8.8
This command runs a container with all capabilities dropped except network admin, and disables privilege escalation, then pings an IP once.
Look for repeated steps or checks during container startup.
- Primary operation: Checking and applying each capability and privilege control setting.
- How many times: Once per capability or privilege option specified.
As you add more capabilities or privilege controls, the container runtime checks each one in turn.
| Input Size (number of capabilities) | Approx. Operations |
|---|---|
| 1 | 1 check |
| 10 | 10 checks |
| 20 | 20 checks |
Pattern observation: The number of checks grows directly with the number of capabilities or privilege options set.
Time Complexity: O(n)
This means the time to apply capabilities and privilege controls grows linearly with how many you specify.
[X] Wrong: "Adding more capabilities or privilege controls does not affect container startup time."
[OK] Correct: Each capability or privilege control requires a check and setup step, so more options mean more work for the runtime.
Understanding how security settings affect container startup helps you balance safety and performance in real projects.
"What if we batch apply capabilities instead of one by one? How would the time complexity change?"
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
