In this tutorial, we aim to shed light on one of the most critical aspects of Docker security: avoiding running Docker containers in privileged mode. By the end of this tutorial, you will be able to run Docker containers with limited privileges, enhancing the security of your Docker environment.
When a container is run in privileged mode, it has all the capabilities of the host machine, which could lead to severe security issues if the container is compromised. This is similar to running a program as root on the host machine.
The key to running Docker containers with limited privileges is to understand and appropriately use Docker’s --cap-add and --cap-drop options. These options allow you to control the capabilities of your Docker containers.
# Run a Docker container with only the necessary capabilities
docker run --cap-drop all --cap-add chown ubuntu
In this example, we are running an Ubuntu container with only the chown capability. The --cap-drop all option drops all capabilities, and the --cap-add chown option adds the chown capability.
# Check the capabilities of a running Docker container
docker exec -it <container_id> capsh --print
Replace <container_id> with the ID of your running Docker container. This command prints the capabilities of the running Docker container.
In this tutorial, you've learned about the risks of running Docker containers in privileged mode and how to avoid them by running containers with limited privileges. You've also learned how to use Docker’s --cap-add and --cap-drop options and how to check the capabilities of a running Docker container.
For further learning, you can explore other Docker security features such as user namespaces, seccomp profiles, and more.
setgid and setuid CapabilitiesHint: Use the --cap-add option to add the setgid and setuid capabilities.
Hint: Use the docker exec -it <container_id> capsh --print command to print the capabilities of the running Docker container.
Hint: Try running the ping command in a Docker container that does not have the net_raw capability.
The solutions for the exercises are as follows:
docker run --cap-drop all --cap-add setgid --cap-add setuid ubuntu
docker exec -it <container_id> capsh --print
If you try to run the ping command in a Docker container that does not have the net_raw capability, you will get an error message. This is because the ping command requires the net_raw capability to function correctly.