Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting a Docker Container to a single cpu core

Tags:

I'm trying to build a system which runs pieces of code in consistent conditions, and one way I imagine this being possible is to run the various programs in docker containers with the same layout, reserving the same amount of memory, etc. However, I can't seem to figure out how to keep CPU usage consistent.

The closest thing I can seem to find are "cpu shares," which, if I understand the documentation, limit cpu usage with respect to what other containers/other processes are running on the system, and what's available on the system. They do not seem to be capable of limiting the container to an absolute amount of cpu usage.

Ideally, I'd like to set up docker containers that would be limited to using a single cpu core. Is this at all possible?

like image 468
mavix Avatar asked Sep 23 '14 15:09

mavix


People also ask

Can I limit file size CPU utilization for Docker in my machine?

Limit Docker Container CPU Usage You can also use the --cpu-shares option to give the container a greater or lesser proportion of CPU cycles. By default, this is set to 1024. To find more options for limiting container CPU usage, please refer to Docker's official documentation.

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.


2 Answers

If you use a newer version of Docker, you can use --cpuset-cpus="" in docker run to specify the CPU cores you want to allocate:

docker run --cpuset-cpus="0" [...] 

If you use an older version of Docker (< 0.9), which uses LXC as the default execution environment, you can use --lxc-conf to configure the allocated CPU cores:

docker run --lxc-conf="lxc.cgroup.cpuset.cpus = 0" [...] 

In both of those cases, only the first CPU core will be available to the docker container. Both of these options are documented in the docker help.

like image 165
dcro Avatar answered Oct 11 '22 12:10

dcro


I've tried to provide a tutorial on container resource alloc.

https://gist.github.com/afolarin/15d12a476e40c173bf5f

like image 29
Amos Folarin Avatar answered Oct 11 '22 14:10

Amos Folarin