Craig
Craig

Reputation: 93

How to limit how many cpus a docker container can see

I am wondering if it's possible to limit how many CPU's / core's that a docker container can see? For example, if I launch a container via:

docker run --rm -it  --cpus=2 ubuntu:20.04

It is still able to see all of the cores on my machine even though I've limited the container to 2 (htop image below for clarification)

enter image description here

This is causing issues in analytical work where libraries such as OpenBLAS as well as several Python / R packages are configured (by default) to use all cores on the machine (in this case 6) which results in the process being unnecessarily throttled. Therefore is it possible to configure the container launch such that the container can only see the cores that it can access?

Upvotes: 4

Views: 2672

Answers (1)

Romain Prévost
Romain Prévost

Reputation: 543

I don't think you can easily hide CPUs from your docker container, this is not a VM.

IMO, your best bet is either to dive into your library configuration to restrict its number of cores used, or use a full blown VM.

Upvotes: 2

Related Questions