Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the CPU priority (niceness) of a Docker container?

Tags:

docker

One of my containers is always busy, and is taking CPU away from other containers (webservers) that need to be responsive and are only active from time to time.

I would like to lower the CPU priority of the CPU-consuming container, so that whenever the other containers need the CPU, it is not clogged.

How do I do this? I have been searching the web for a while now, but I can't find the answer.

I have tried running the container with --entrypoint='nice 10 mybinary', but it turns out --entrypoint can only run binaries, not shell commands.

like image 968
kovan Avatar asked Jan 27 '19 19:01

kovan


People also ask

How do you limit a Docker container memory and CPU?

To limit the maximum amount of memory usage for a container, add the --memory option to the docker run command. Alternatively, you can use the shortcut -m . Within the command, specify how much memory you want to dedicate to that specific container.

How can CPU be used in Docker container?

We can get CPU usage of docker container with docker stats command. The column CPU % will give the percentage of the host's CPU the container is using.

What is CPU quota Docker?

0 of Docker onward. The --cpu-quota option specifies the number of microseconds that a container has access to CPU resources during a period specified by --cpu-period. As the default value of --cpu-period is 100000, setting the value of --cpu-quota to 25000 limits a container to 25% of the CPU resources.

How much CPU can a Docker container use?

On windows, a container defaults to using two CPUs. If hyperthreading is available this is one core and two logical processors. If hyperthreading is not available this is two cores and two logical processors.


1 Answers

You can limit CPU resources on the container level. I recommend to use --cpu-shares 512 for your case.

https://docs.docker.com/config/containers/resource_constraints/:

Set this flag to a value greater or less than the default of 1024 to increase or reduce the container’s weight, and give it access to a greater or lesser proportion of the host machine’s CPU cycles. This is only enforced when CPU cycles are constrained. When plenty of CPU cycles are available, all containers use as much CPU as they need. In that way, this is a soft limit. --cpu-shares does not prevent containers from being scheduled in swarm mode. It prioritizes container CPU resources for the available CPU cycles. It does not guarantee or reserve any specific CPU access.

like image 124
Jan Garaj Avatar answered Oct 25 '22 20:10

Jan Garaj