8

I have been working with Docker for a while now, I have installed docker and launched a container using

docker run -it --cpuset-cpus=0 ubuntu

When I log into the docker console and run

grep processor /proc/cpuinfo | wc -l

It shows 3 which are the number of cores I have on my host machine.

Any idea on how to restrict the resources to the container and how to verify the restrictions??

Matt
  • 68,711
  • 7
  • 155
  • 158
user3398900
  • 795
  • 2
  • 13
  • 31
  • Does this answer your question? [How many CPUs does my docker container have?](https://stackoverflow.com/questions/49151296/how-many-cpus-does-my-docker-container-have) – Michael Freidgeim Oct 12 '21 at 20:50

4 Answers4

13

The issue has been already raised in #20770. The file /sys/fs/cgroup/cpuset/cpuset.cpus reflects the correct output.

The cpuset-cpus is taking effect however is not being reflected in /proc/cpuinfo

yamenk
  • 46,736
  • 10
  • 93
  • 87
7
docker inspect <container_name> 

will give the details of the container launched u have to check for "CpusetCpus" in there and then u will find the details.

user3398900
  • 795
  • 2
  • 13
  • 31
5

Containers aren't complete virtual machines. Some kernel resources will still appear as they do on the host.

In this case, --cpuset-cpus=0 modifies the resources the container cgroup has access to which is available in /sys/fs/cgroup/cpuset/cpuset.cpus. Not what the VM and container have in /proc/cpuinfo.

One way to verify is to run the stress-ng tool in a container:

Using 1 cpu will be pinned at 1 core (1 / 3 cores in use, 100% or 33% depending on what tool you use):

docker run --cpuset-cpus=0 deployable/stress -c 3

This will use 2 cores (2 / 3 cores, 200%/66%):

docker run --cpuset-cpus=0,2 deployable/stress -c 3

This will use 3 ( 3 / 3 cores, 300%/100%):

docker run deployable/stress -c 3

Memory limits are another area that don't appear in kernel stats

$ docker run -m 64M busybox free -m
             total       used       free     shared    buffers     cached
Mem:          3443       2500        943        173        261       1858
-/+ buffers/cache:        379       3063
Swap:         1023          0       1023

yamaneks answer includes the github issue.

Matt
  • 68,711
  • 7
  • 155
  • 158
-2

it should be in double quotes --cpuset-cpus="", --cpuset-cpus="0" means it make use of cpu0.

sanath meti
  • 5,179
  • 1
  • 21
  • 30
  • # docker run -it --cpuset-cpus="0" 20c44cd7596f "/bin/bash" root@c44351fd21a7:/# grep processor /proc/cpuinfo | wc -l 3 No change – user3398900 Nov 29 '17 at 06:05
  • 1
    Quotes are only required if you have spaces or other shell special characters in the argument. – Matt Nov 29 '17 at 08:10