Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allocate 50% CPU resource to docker container via `docker run`?

Tags:

docker

I have a 4-core CPU, I want to allocate 50% CPU resource to a docker container.
After reading the docker-run manual and config.go source code.
I still don't know how to use the -c, --cpu-shares=0 option.

docker run -c 0.5 -i -t ubuntu /bin/bash 

Or

docker run -c 2 -i -t ubuntu /bin/bash 
like image 252
kev Avatar asked Nov 10 '14 10:11

kev


People also ask

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. On linux, a container defaults to using all available CPUs of the host.

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 RAM should I allocate to Docker?

Limit a container's access to memory The maximum amount of memory the container can use. If you set this option, the minimum allowed value is 6m (6 megabytes). That is, you must set the value to at least 6 megabytes. The amount of memory this container is allowed to swap to disk.


1 Answers

cpu-shares is a 'relative weight', relative to the default setting of 1024, so if you had two containers running on the same core, you could give them the CPU 50-50 or 80-20 or whatever you wanted by adjusting the numbers. It is an integer.

You cannot give an overall limit, as you want to, using this flag, but you can restrict the set of CPUs that the container runs on using --cpuset mentioned here.

The number 1024 is in the Cgroups docs.

This blog post from Marek Goldmann explains resource management in Docker.

See also Setting absolute limits on CPU for Docker containers, which says it can be done with lxc (older Docker implementation) but not libcontainer (current Docker implementation).

like image 187
Bryan Avatar answered Oct 05 '22 03:10

Bryan