Docker & swap space

Hi,

As each running docker container uses the host Kernel, they also use the memory and swap of the host. If this is a one of requirement its better to increase the host swap space.

If you want to still add swap from the container you have two options.

  • Run container in privileged mode
    In this case you will have to run the container with --privileged option.
# Example
docker run -it --rm --privileged centos:6
  • Running container with privileged mode gives container full privilege on the host. If you read the manpage of swapon you can see that for swapon to run the process should have CAP_SYS_ADMIN capability. In Docker you can add a particular capability selectively to the container using the --cap-add argument.
# Example
docker run -it --rm --cap-add SYS_ADMIN centos:6

If you run the container in either of the above two modes you can achieve what you are trying.

Now the problem with this approach is , when you create swap inside the container and start using that, its actually the Host Kernel that is using it, as a result when you exit the container without a swapoff the host kernel will be still using the file, and you wont get a clean exit of the container. You will see a dead status for the container.

Hope this helps. The following link helped me in understanding this, https://goo.gl/h60ico.

2 Likes