Docker

How to Set Ulimits or File Descriptor on Docker Container

In Docker, a file descriptor is used to represent an open file, socket, or other input/output resource. By default, Docker containers access or use the Docker daemon’s file descriptor. Still, sometimes, it is required to set the ulimits or file descriptor for containers manually to limit the resource usage for containers or to point to a specific file, such as a database that may be needed to store or access a specific file.

This blog will illustrate how to set the ulimit or file descriptor on the Docker container.

How to Set Ulimits or File Descriptor on Docker Container?

To set the ulimits or file descriptor on the Docker container, first, create an image. After that, create and set the ulimits or file descriptor on the Docker container using the “–ulimit” option. To set the ulimits or file descriptor on the Docker container, go through provided steps.

Step 1: Define Dockerfile Instructions

First, create a Dockerfile that includes the instructions to dockerize the application. For instance, we have dockerized the “index.html” program:

FROM nginx:latest

COPY index.html /usr/share/nginx/html/index.html

ENTRYPOINT ["nginx", "-g", "daemon off;"]

Step 2: Generate Docker Image

Next, generate the new Docker image by utilizing the Dockerfile instructions through the given command:

docker build -t html-img .

Step 3: Build Container and Set Ulimits of Container

After that, build and run the container using the “docker run” command. Also, set the file descriptor or ulimits on the container using the “–ulimit” option and set its value as “nofile=soft-ulimit:hard:ulimit”:

docker run -p 80:80 --ulimit nofile=46465:46465 --name html-container html-img

Check if the container is executing or not by navigating to the assigned port of the local host:

Step 4: Check Container’s Ulimit

In order to check if the ulimits is set on a Docker container or not, run the “docker exec” command. Here, the “-it” option is utilized to run the specified container interactively and allocate the TTY-pseudo terminal:

docker exec -it html-container sh

After that, check the containers ulimits by executing the “ulimit -n” command:

ulimit -n

Here, you can see we have successfully set the file descriptor on Docker container:

This is all about setting ulimits or file descriptors on Docker containers.

Conclusion

To set the ulimits or file descriptor on the Docker container, first, create the Docker images by utilizing the Dockerfile instructions. After that, build the container and set the ulimits for the container using the “–ulimit” option along with the “docker run” command. To set the file descriptor, specify the “–ulimit” value as “nofile=soft-ulimit:hard-ulimit”. This write-up has demonstrated how to set the ulimit or file descriptor on the Docker container.

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.