Docker

How to Set the Docker ulimits

Docker is a well-known free and open-source development environment for building, deploying, and running applications in small executable packages. This platform gives developers more control over containerized applications and system resources. It allows us to set the ulimit on the containerized application to limit the number of resources for the container, such as the number of files the container can open, the number of processors, and the memory container can use.

This write-up will describe how to set the Docker ulimits.

How to Set the Docker ulimits?

Setting ulimits is a process of allocating the system resources to users or containers. This feature of Docker is helpful in controlling resource usage and avoiding container performance issues. To set the ulimit in Docker to limit the resources for the container, follow the listed steps.

Step 1: Check Default Ulimit

Firstly, run the containers and check the default set ulimit of Docker Daemon in Docker using the below command:

docker run --rm debian sh -c "ulimit -n"

 
In the above command, “–rm” is used to automatically remove the container after execution, “debian” is a Docker image, and “sh” is utilized to run the command “ulimit -n” to view the limit:


Step 2: Launch Docker

After that, move toward the Docker Desktop application. For this purpose, open the Docker Desktop app from the Start menu:


Step 3: Visit Docker Desktop Settings

Click on the below highlighted “Settings” icon to open the Docker settings:


Step 4: Open Docker Engine Settings

From the left bar, visit the “Docker Engine” settings. Here, you can find the Daemon.json default file configurations as highlighted below:


Step 5: Set Ulimits of Docker

Add the following Json configurations to the “daemon.json” file. These instructions are setting the ulimit on a number of open files. We have set hard and soft ulimit to “65535”:

"default-ulimits": {
 "nofile": {
  "Hard": 65535,
  "Name": "nofile",
  "Soft": 65535,
 }
}

 

Note: You can see any type of ulimit in Docker Daemon, such as “nproc”, used to limit the number of processors and memory limit:

"default-ulimits": {
  "nofile": 65535,
  "nproc": 2048
}

 
Step 6: Verification

After that, again, run the container using the debian image and check the ulimit of the container:

docker run --rm debian sh -c "ulimit -n"

 
Here, you can see we have successfully set the ulimit of a number of the open file in containers:


That’s all about setting the Docker ulimits.

Conclusion

To set the ulimits in Docker to limit the resource usage, first, open the “daemon.json” default file configurations from the “Docker Engine” settings menu. Next, set the ulimit in “daemon.json” files like “{ “default-ulimits”: { “nofile”: { “Name”: “nofile”, “Hard”: VALUE, “Soft”: VALUE, } }}”. This blog has illustrated how to set the Docker ulimits to limit resource usage.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.