How to Apply Resource Limits Via limits.conf for a Docker Container
There are several ways in which resource limits can be applied to the docker container. Majorly below 3 ways can be used to set resource limits globally or individually for each container.
Setting Global limits for all containers
1. In docker 1.6 or later, we could set ulimit in /etc/sysconfig/docker as:
1 2 | # vi /etc/sysconfig/docker OPTIONS='--insecure-registry=172.30.0.0/16 --selinux-enabled --default-ulimit nproc=1024:2048' |
This will set a soft limit of 1,024 and a hard limit of 2,048 child processes for all containers.
2. Or you could set it in systemd. Docker service(/usr/lib/systemd/system/docker.service) is started using the following parameters by default.
1 | # cp /usr/lib/systemd/system/docker.service /etc/systemd/system/docker.service |
1 2 3 | # vi /etc/systemd/system/docker.service LimitNOFILE=1048576 LimitNPROC=1048576 |
so the docker containers will get the nproc from the docker service.
3. Adjust the values and then restart docker service and then start the containers. Only the non-root user processes within Docker containers will be limited.
1 2 | # systemctl daemon-reload # systemctl restart docker |
As per the systemd documentation, following ulimit settings can be set via docker.service file:
1 2 3 | LimitCPU=, LimitFSIZE=, LimitDATA=, LimitSTACK=, LimitCORE=, LimitRSS=, LimitNOFILE=, LimitAS=, LimitNPROC=, LimitMEMLOCK=, LimitLOCKS=, LimitSIGPENDING=, LimitMSGQUEUE=, LimitNICE=, LimitRTPRIO=, LimitRTTIME= |
Change specific container limit
In this case please run the container with the following option:
1 | # docker run -it --ulimit nofile=122880:122880 centos |
here,
122880 is the hard and soft open files limit for the container “centos”.
Alternate approach (using prlimit)
1. Find the pid of container processes from the host system.
2. set the limits using the prlimit command. For Example:
1 | # prlimit -p 31143 --nproc=65535:65535 |
3. To query the current value
1 | # prlimit -p 31143 --nproc |
I hope it has been a useful article.