🐳Docker Linux Capabilities Labs

🎯 Objective

Learn what Linux capabilities are, how containers use them, and how to manage them securely using Docker’s --cap-add and --cap-drop options.


🧰 Prerequisites

  • Docker installed (docker version should work)

  • Root or sudo access to run containers

  • Tools: capsh, iputils, libcap2-bin

sudo apt update
sudo apt install libcap2-bin iputils-ping

πŸ”Ή Lab 1: View Capabilities Inside a Docker Container

Step 1: Create and Start a custom container

Create this Dockerfile using vim or nano editors:

FROM ubuntu:latest
RUN apt update && apt install -y iputils-ping libcap2-bin
CMD ["bash"]

Build the docker container using:

Step 2: Check capabilities

βœ… Expected: Default set of capabilities shown (cap_net_raw, cap_chown, etc.)

Excursive: 🧠 Capsh Section Breakdown

capsh_breakdown

You may also use the more easier approach to see the effective capabilities using this command:


πŸ”Ή Lab 2: Drop All Capabilities

Step 1: Run a container with no capabilities

Step 2: Check no capabilities are set

Step 3: Try actions that need privileges

❌ Expected: ping fails because cap_net_raw is missing.


πŸ”Ή Lab 3: Add a Specific Capability

Step 1: Add back CAP_NET_RAW

Step 2: Check NET_RAW capability is set

Step 3: Try ping again

βœ… Expected: ping works with CAP_NET_RAW restored.


πŸ”Ή Lab 4: Compare with Privileged Containers

Step 1: Run a privileged container

βœ… Expected: All SYS_ADMIN capability is available. The container can do nearly anything the host can.

Step 2: Try mounting inside a container

βœ… Expected: Works with CAP_SYS_ADMIN; fails without it.

Step 3: Check access to all (host) devices

βœ… Expected: Privileged containers have access to almost all host devices (/dev/kvm, /dev/sda, etc.), unlike default containers.


πŸ”Ή Lab 5: Inspect Running Containers

Step 1: Run a container in the background

Step 2: Inspect the container

βœ… Expected: CapDrop should list all dropped caps, CapAdd should be empty.


βœ… Wrap-Up

  • You explored capabilities in Docker and saw how they restrict or allow actions.

  • You ran containers with minimal privileges (--cap-drop=ALL).

  • You added capabilities only as needed (--cap-add=...).

  • You compared privilege escalation using --privileged.


Last updated