How to Set Docker Memory and CPU Usage Limit?

Photo of author

By admin

Docker containers by default have full access to the RAM and CPU of the host. Leaving them to the default settings might result in bottlenecked performance i.e. you will notice slightly degraded performance. This article will cover how to set Docker memory and CPU usage limits.

Why Limit Docker Memory and CPU Usage?

If you don’t limit the Docker memory and CPU usage, Docker might use all the resources available. This can result in lags and freeze issues, which will surely affect your experience.

When a Docker container is used without limit, it will have access to all the system resources, which will lead to a shortage of memory for other containers. To resolve this, you will need to enforce some limits. This will then ensure to treat all the containers equally. In the next section, we will guide you step-by-step to set Docker memory and CPU usage limits.

How to Set Docker Memory and CPU Usage Limit?

Step 1. Configure the System to Enable Limiting of Resources

First of all, you need to check whether your system supports the Docker option to run a container with limited resources.

  • To check this, run the command below:

sudo docker info

  • If you receive the output saying: WARNING: No swap limit support, means that limit is not enabled by default.

To enable it, you will need to edit the grub configuration file via any text editor of your choice.

  • Type the command below to proceed further and edit the grub configuration file:

sudo nano /etc/default/grub

  • Now under this line – GRUB_CMDLINE_LINUX_DEFAULT=“quiet splash”, add the code below:

GRUB_CMDLINE_LINUX=”cdgroup_enable=memory swapaccount=1″

  • Now you can save and exit the text editor.
  • Next, you need to update the grub configuration by using the command below:

sudo update-grub

  • Then you need to reboot your PC.
  • Once rebooted, you can then confirm changes by typing the command below:

sudo docker info

Step 2. Limit Docker Container Memory Access

There are various RAM limitations you can set for Docker containers, such as:

  1. Configuring the maximum memory for a container.
  2. Setting up the memory for Docker containers to swap with the disk.
  3. Set the soft limit of memory assigned to a container.

1. Configure Maximum Memory Access

You can use the command below to proceed further to set the maximum memory for a Docker container:

sudo docker run -it –memory=”[memory_limit]” [docker_image]

[Note: You will need to replace “memory_limit” with the size you want to allot to the container. Moreover, replace the “docker_image” with your server name. On the other hand, you can allot the memory by using the initials as b, k, m, or g for Bytes, Kilo bytes, Mega bytes, or Giga bytes.]

2. Set Swap to Disk Memory Limit

This will allow you to store data even if your assigned RAM storage is full. It then just ignores the limitations and starts writing directly to the disk. You can run the syntax below for running a container with limited memory and additional swap memory:

sudo docker run -it –memory=”[memory_limit]” –memory-swap=”[memory_limit]” [docker_image]

3. Set Soft Limit for Container

By setting the soft limit for the container, you will get a warning when the container reaches the maximum capacity of its allotted memory. You will just need to use the command below to set the soft limit:

sudo docker run -it –memory=”1g” –memory-reservation=”750m” [docker_image]

Step 3. Limit the Docker Container CPU Usage

If we allow containers for unlimited CPU usage (CPU cores), it can also lead to similar issues as screen freezes, lags and some uncommon PC behavior.

  • To limit the CPU usage of the Docker container, you can use the command below:

sudo docker run -it –cpus=”1.0″ [docker_image]

[Note: Here you can replace 1.0 with the number of CPUs you want to limit for the Docker container, similar to the memory access limits.]

You can even allot a greater or lesser proportion of the CPU cycles. It is set as 1024 by default.

  • To customize the proportion as per your preference you can use the command below:

sudo docker run -it –cpus-shares=”700″ [docker_image]

Conclusion

We hope that after following this article you get to know how to set Docker memory and CPU usage limits. If you have any doubt, feel free to ask in the comments section below. We will try to resolve it as soon as possible.

Leave a Comment