Adding Capabilities with Docker
Granting Capabilities to a Container
To add capabilities to a Docker container, you can use the --cap-add
flag when running the docker run
command. This allows you to specify the capabilities you want to grant to the container.
docker run --cap-add=<capability> <image>
For example, to grant the CAP_NET_ADMIN
capability to a container, you would run:
docker run --cap-add=NET_ADMIN ubuntu:22.04 /bin/bash
Granting Multiple Capabilities
You can grant multiple capabilities to a container by specifying the --cap-add
flag multiple times:
docker run --cap-add=NET_ADMIN --cap-add=SYS_ADMIN ubuntu:22.04 /bin/bash
This will grant both the CAP_NET_ADMIN
and CAP_SYS_ADMIN
capabilities to the container.
Viewing Granted Capabilities
You can view the capabilities granted to a running container using the docker inspect
command:
docker inspect <container_id> | grep Capabilities
This will output the list of capabilities granted to the container.
"Capabilities": {
"Bounding": [
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FOWNER",
"CAP_FSETID",
"CAP_KILL",
"CAP_NET_BIND_SERVICE",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETPCAP",
"CAP_NET_ADMIN"
],
"Effective": [
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FOWNER",
"CAP_FSETID",
"CAP_KILL",
"CAP_NET_BIND_SERVICE",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETPCAP",
"CAP_NET_ADMIN"
],
"Inheritable": [
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FOWNER",
"CAP_FSETID",
"CAP_KILL",
"CAP_NET_BIND_SERVICE",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETPCAP",
"CAP_NET_ADMIN"
],
"Permitted": [
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
"CAP_FOWNER",
"CAP_FSETID",
"CAP_KILL",
"CAP_NET_BIND_SERVICE",
"CAP_NET_RAW",
"CAP_SETGID",
"CAP_SETUID",
"CAP_SETPCAP",
"CAP_NET_ADMIN"
]
}
By understanding how to add capabilities to a Docker container, you can enhance the security and functionality of your containerized applications.