Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit how many cpus a docker container can see

Tags:

docker

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?

like image 250
Craig Avatar asked Sep 19 '25 10:09

Craig


1 Answers

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.

like image 95
Romain Prévost Avatar answered Sep 23 '25 06:09

Romain Prévost