5

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?

user14717
  • 4,757
  • 2
  • 44
  • 68
  • 2
    `getconf _NPROCESSORS_ONLN` might be the shortest way to do it, I believe that's POSIX and should work on OS X and Linux. [related question](http://stackoverflow.com/questions/19619582/get-the-number-of-processors-cores-in-command-line) – Kevan Ahlquist Sep 13 '15 at 23:01
  • If you want to use *all* the CPUs, why go for `cpu_count()-1` and not `cpu_count()`? – Donal Lafferty Oct 21 '16 at 08:47

1 Answers1

1

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"
feco
  • 4,384
  • 5
  • 27
  • 44