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:

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.

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.

As per the systemd documentation, following ulimit settings can be set via docker.service file:

Change specific container limit

In this case please run the container with the following option:

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:

3. To query the current value


I hope it has been a useful article.