简体   繁体   中英

Docker container CPU allocation

I have created a container:

docker run -c=20 -i -t  ubuntu:latest /bin/bash

I tried to use -c flag to control CPU usage and maximize it in 50 %. When I am running md5sum /dev/urandom inside container, it use up 100 % CPU in host machine.

The -c flag for docker run command modifies the container's CPU share weighting relative to the weighting of all other running containers.

It does not restrict the container's use of CPU from the host machine.

You can use the --cpu-quota flag to limit CPU usage, for example:

$ docker run -ti  --cpu-quota=50000 ubuntu:latest /bin/bash

The --cpu-quota is usually used in conjunction with --cpu-period . Please see more details on the Docker run reference document:

https://docs.docker.com/reference/run/#runtime-constraints-on-resources

It seems that you are running a single container, so this is the expected result.

You might find this blog post helpful.

Every new container will have 1024 shares of CPU by default. This value does not mean anything, when speaking of it alone. But if we start two containers and both will use 100% CPU, the CPU time will be divided equally between the two containers because they both have the same CPU shares (for the sake of simplicity I assume that there are no other processes running).

Take a look here, this is apparently what you were looking for:

https://docs.docker.com/engine/reference/run/#cpu-period-constraint

The default CPU CFS (Completely Fair Scheduler) period is 100ms. We can use --cpu-period to set the period of CPUs to limit the container's CPU usage. And usually --cpu-period should work with --cpu-quota.

Examples:

$ docker run -it --cpu-period=50000 --cpu-quota=25000 ubuntu:14.04 /bin/bash

If there is 1 CPU, this means the container can get 50% CPU worth of run-time every 50ms.

period and quota definition:

Within each given "period" (microseconds), a group is allowed to consume only up to "quota" microseconds of CPU time. When the CPU bandwidth consumption of a group exceeds this limit (for that period), the tasks belonging to its hierarchy will be throttled and are not allowed to run again until the next period.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM