Get the App
SLTechnology News&Howtos  ›  Servers  › 

Docker restricts the use of CPU by containers

Shulou Source: shulou.com Published: 2022-06-02 03:38:12 10月30日 Update

In the previous section, you learned how to limit the use of memory by containers. In this section, we take a look at CPU.

By default, all containers can use host CPU resources equally and without restrictions.

Docker can set the weight of CPU used by the container with-c or-- cpu-shares. If not specified, the default value is 1024.

Unlike the memory limit, the cpu share set with-c is not the absolute number of CPU resources, but a relative weight value. The CPU resources that a container can eventually be allocated depend on its cpu share as a percentage of the total cpu share of all containers.

In other words: through cpu share, you can set the priority for the container to use CPU.

For example, two containers are launched in host:

Docker run-name "container_A"-c 1024 ubuntudocker run-name "container_B"-c 512 ubuntu

Container_A 's cpu share 1024 is twice that of container_B. When both containers require CPU resources, container_A can get twice as much CPU as container_B.

It is important to note that this weighted CPU only occurs when CPU resources are tight. If container_A is idle, container_B can also be allocated to all available CPU in order to make full use of CPU resources.

Let's continue to experiment with progrium/stress.

1. The startup container_A,cpu share is 1024:

Cpu is used to set the number of worker threads. Because there is only one CPU in the current host, a worker thread can fill up the CPU. If the host has more than one CPU, you need to increase the number of cpu accordingly.

2. The startup container_B,cpu share is 512:

3. Execute top in host to check the container's use of CPU:

Container_A consumes twice as much CPU as container_B.

4. Pause container_A now:

5. Top shows that container_B can use the whole CPU when container_A is idle:

CPU limit is discussed here, the above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Tags: Container resource situation quantity weight allocation limit two idle thread study work different equality priority memory content depending on only can be passed Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Docker Xiaomi Shulou Technology