Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less awkward method of using all available CPUs in Docker container:

Tags:

docker

I'm using the following hideous syntax to tell my docker container to use all the CPUs on the host machine:

docker run  --cpuset-cpus="0-`python3 -c "from multiprocessing import cpu_count; print(cpu_count() -1)"`" ubuntu:latest /bin/bash

Is there a better way?

like image 918
user14717 Avatar asked Nov 10 '22 05:11

user14717


1 Answers

You can use nproc to return the number of CPU cores.

In order to get the number of CPU cores - 1, Arithmetic in POSIX shells is done with $ and double parentheses

docker run --cpuset-cpus="0-$(($(nproc)-1))" ubuntu:latest echo "hi"
like image 81
feco Avatar answered Nov 15 '22 06:11

feco