top of page

Maximizing Your Docker Container CPU Efficiency: How to Set CPU Limits

Docker allows you to limit the CPU resources that a container can use. This is useful for preventing a single container from consuming all available CPU resources on a host machine. You can set CPU limits when running a container using one of the below methods. So lets understand How to Set CPU Limits.let's

1. Using --cpus Option:

The --cpus option in Docker is used to limit the amount of CPU resources a container can use. It allows you to specify the number of CPUs that a container can access. This option is useful for controlling and distributing CPU resources among multiple containers running on the same host machine.

Here's an example of how you can use the --cpus option:

[root@siddhesh ~]# docker   run -dt -p 443:80 --cpus=2 nginx
[root@siddhesh ~]#

In this example, the container nginx is limited to using a maximum of 2 CPU cores. You can adjust the value after --cpus to set the desired number of CPU cores for your container.

[root@siddhesh ~]# docker inspect 74575a6e85c4eb2890b543b5525f0d6330b1dfb5c7b7573c9b964b2a5af8a236|grep CpuCount
            "CpuCount": 2,
[root@siddhesh ~]#

2. Using --cpu-quota and --cpu-period Option:

The --cpu-quota and --cpu-period options in Docker are used to control and limit the CPU usage of a container. These options are more fine-grained than the --cpus option, providing more control over how CPU resources are allocated to a container. --cpu-quota:

  • This option sets the maximum amount of CPU time, in microseconds, that a container can use within a specified time period (--cpu-period).

  • The value for --cpu-quota is specified in microseconds, and it represents the total CPU time that the container can use in a period of --cpu-period.


  • This option sets the length of the time period (in microseconds) during which the container can use the CPU time specified by --cpu-quota.

  • It defines the time window for which the CPU usage is measured.

[root@siddhesh ~]# docker run -dt -p 443:80 --cpu-quota=100000 --cpu-period=50000 nginx
[root@siddhesh ~]#

In this example, the time period is set to 50,000 microseconds (0.05 seconds), and the container can use a maximum of 100,000 microseconds (0.1 seconds) of CPU time within that period.

[root@siddhesh ~]# docker inspect 0d16fbb17adb91531b79e1471c956767ff90ef422a9e3f38457acc5497aac076|egrep 'CpuPeriod|CpuQuota'
            "CpuPeriod": 50000,
            "CpuQuota": 100000,
[root@siddhesh ~]#

3. Using --cpu-shares Option: Using the docker run command with the --cpu-shares flag. This option is used to specify a container's relative CPU shares. This implies that if many containers are executing on the same host, the container with the highest CPU share value will receive a bigger part of the CPU resources.

[root@siddhesh ~]# docker run -dt -p 443:80 --cpu-shares 512 nginx
[root@siddhesh ~]#

In this example, the container is allocated 512 CPU shares. The relative weight of the CPU shares determines the container's priority for CPU resources compared to other containers on the same host. Containers with higher share values get proportionally more CPU time when the host is under contention.

[root@siddhesh ~]# docker inspect 77e3a2f00c27f234143c0677088abe1e52b63a512e63bf2ab80acdc316de251b|grep CpuShares
            "CpuShares": 512,
[root@siddhesh ~]#

4. Using --cpuset-cpus Option:

The --cpuset-cpus option in Docker allows you to limit the container to a specific set of CPU cores. This is useful when you want to control and restrict the execution of a container to a particular subset of CPU cores on the host machine.

[root@siddhesh ~]# docker run -dt -p 443:80 --cpuset-cpus 0-2 nginx
[root@siddhesh ~]#

In this example, the container is limited to running on CPU cores 0, 1, and 2. The argument to --cpuset-cpus is a comma-separated list or a range of CPU cores.

[root@siddhesh ~]# docker inspect f0be5a1c5cb6f96b143b358ccf6ab7237d1aa95e0adcab24bb46bda576c55647|grep CpusetCpus
            "CpusetCpus": "0-2",
[root@siddhesh ~]#

Reduce Assigned CPU Set:

[root@siddhesh ~]# docker update --cpuset-cpus 0-1 f0be5a1c5cb6f96b143b358ccf6ab7237d1aa95e0adcab24bb46bda576c55647
[root@siddhesh ~]#

This command updates the CPU affinity for nginx container to run on CPU cores 0 and 1.

Note : Keep in mind that the actual availability of CPU cores depends on the host machine's configuration. If you specify more CPU cores than are available, Docker will raise an error.

bottom of page