3

I have a backend server program that launches docker containers using the Docker Engine API (Json API) and I would like to limit the number of CPU cores per docker container to e.g. 1.

The Docker Engine API documentation has several options to configure the CPU settings of a container, see https://docs.docker.com/engine/api/v1.24:

"HostConfig": {
     "CpuPercent": 80,
     "CpuShares": 512,
     "CpuPeriod": 100000,
     "CpuQuota": 50000,
     "CpusetCpus": "0,1",
     "CpusetMems": "0,1"
}

I could use CpusetCpus for this but this is quite cumbersome as I need to keep a list of which docker containers are assigned to which CPU cores. (I don't care on which CPU core my container runs, I just want to avoid that it uses more than 1 CPU core.)

I could also set the CpuQuota so that each docker container uses only e.g. 12.5% of all CPU cores, which corresponds to 1 CPU core on a server with 8 CPU cores. The problem with this approach is that if I run my backend program on a different server with a different number of CPU cores the CpuQuota setting of 12.5% does not correspond to 1 CPU core anymore.

Both options above are cumbersome and far from ideal. There must be a better/simpler way to set the number of CPU cores per container?!

Linoliumz
  • 2,381
  • 3
  • 28
  • 35

1 Answers1

6

You can set NanoCPUs. 1000000000 units would equal 1 core.

King Chung Huang
  • 5,026
  • 28
  • 24
  • Awesome, that's exactly what I was looking for :-) – Linoliumz Sep 19 '18 at 07:04
  • Hi! Quick question — is this a reservation (i.e. the container will be reserved this amount of CPU space and block other containers from using it) or a limit (i.e. the container just can't go over it, but other containers can use the CPU space if they need to)? Thanks! – Pal Kerecsenyi Dec 13 '20 at 19:34
  • Note that this is exactly what `docker`’s `--cpus` option does. – Skippy le Grand Gourou Jul 10 '23 at 08:08