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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With