How many processors does my docker container have?

I am writing a library that runs in parallel. This library is often used in docker containers. I would like to start as many threads as my docker container has dedicated cores.

Does docker support CPU limits somewhere like an environment variable?

For example, if my user installs two processors when creating a container:

docker run --cpuset-cpus="2" myapp:latest 

(see this question )

How can I return number 2 from the container by checking the status of the container?

+5
source share
3 answers

With --cpuset-cpus="2" you are actually using 1 processor. For example, if you have 4 available: {0,1,2,3} , you must specify 2 of them, separating them with a comma or specifying a range. From docs :

--cpuset-cpus

Limit the specific processors or cores that the container can use. A list of comma-separated or comma-separated CPU ranges that the container can use if you have multiple processors. The first processor is numbered 0. The valid value can be 0-3 (for using the first, second, third, and fourth CPUs) or 1.3 (for using the second and fourth CPUs).

Answer:

  • To get the number of processing blocks available: nproc
  • To get the cpu set: /sys/fs/cgroup/cpuset/cpuset.cpus

a small example follows:

  • host:

     $ nproc 4 
  • container with 1 cpu:

     $ docker run --rm -it --cpuset-cpus="2" ubuntu root@73844de506db :/# nproc 1 root@73844de506db :/# cat /sys/fs/cgroup/cpuset/cpuset.cpus 2 
  • container with 3 cpus:

     $ docker run --rm -it --cpuset-cpus="0-2" ubuntu root@4c3f841e613b :/# nproc 3 root@4c3f841e613b :/# cat /sys/fs/cgroup/cpuset/cpuset.cpus 0-2 
+2
source

Container dockers are isolated by design from the host system, so you won’t be able to ask Docker how many processor cores I have.

Using / proc / cpuinfo does not seem to work for me, it always tells me 1 processor per container.

Looking at this question on Github describes a journey to figure out a workaround. Ultimately, this method examines the group to determine which kernels are available, and I think this will work for you:

cat /sys/fs/cgroup/cpuset/cpuset.cpus

0
source

Files such as / proc / cpuinfo, / proc / meminfo, etc., are not replaced with names. In containers, the best way would be to check /sys/fs/cgroup/cpuset/cpuset.cpus for available

0
source

Source: https://habr.com/ru/post/1275755/


All Articles