Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many CPUs does my docker container have?

Tags:

docker

I'm writing a library that operates in parallel. This library is often used within docker containers. I would like to start as many threads as my docker container has allocated cores.

Does docker set the CPUs limit somewhere as an environment variable?

For example if my user sets two CPUs when creating the container:

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

(see this question)

How do I get back the number 2 from within the container by inspecting the container's state?

like image 387
MRocklin Avatar asked Mar 07 '18 11:03

MRocklin


People also ask

How do I find my Docker CPU?

The first command to check is docker container inspect . With --format , you can even extract its value directly if exposed. Then, at runtime, check docker stats to see if, at runtime, the percentage of the host's CPU and memory the container is using.

How do you check Docker container CPU usage?

Run the docker stats command to display the status of your containers. Memory is listed under the MEM USAGE / LIMIT column. This provides a snapshot of how much memory the container is utilizing and what it's memory limit is. CPU utilization is listed under the CPU % column.

Do Docker containers have their own CPU?

No. Docker image/container only has the application layer of the OS and uses the kernel and CPU of the host machine. That's why docker container boot's so fast. In your host machine kernel is already running, so if you boot your docker container it will share the running kernel and start the container so fast.

What is CPU in Docker stats?

CPU% stats​ CPU is expressed as a percentage (%) of the overall host capacity. One can optimize the resource usage of Docker hosts by being aware of how much CPU the hosts and containers consume. One active/busy container shouldn't slow down other containers by consuming all of the CPU resources.


1 Answers

With --cpuset-cpus="2" you actually use 1 cpu. For example, if you have 4 available: {0,1,2,3} you have to specify 2 of them by separating them with comma or by defining a range. From the docs:

--cpuset-cpus

Limit the specific CPUs or cores a container can use. A comma-separated list or hyphen-separated range of CPUs a container can use, if you have more than one CPU. The first CPU is numbered 0. A valid value might be 0-3 (to use the first, second, third, and fourth CPU) or 1,3 (to use the second and fourth CPU).

Answer:

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

small example follows:

  1. host:

    $ nproc
    4
    
  2. 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
    
  3. 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
    
like image 120
tgogos Avatar answered Oct 15 '22 02:10

tgogos