Reputation: 40624
I have done the following test and I find that I cannot use psutil
to retrieve meaningful data.
Basically I am trying to run this command to use python to report the available memory inside the container.
I have run the test three times and each time with a different memory parameter:
Here are the tests:
docker run -m 10000000 -ti python:2.7 bash
docker run -m 100000000 -ti python:2.7 bash
docker run -m 800000000 -ti python:2.7 bash
and here is the outcome
> docker run -m 10000000 -ti python:2.7 bash
root@310de7b416cc:/# pip install psutil && python -c "import psutil; print psutil.virtual_memory().available"
Killed
> docker run -m 100000000 -ti python:2.7 bash
root@e7aade23c143:/# pip install psutil && python -c "import psutil; print psutil.virtual_memory().available"
Collecting psutil
Downloading psutil-5.3.1.tar.gz (397kB)
100% |████████████████████████████████| 399kB 2.8MB/s
Building wheels for collected packages: psutil
Running setup.py bdist_wheel for psutil ... done
Stored in directory: /root/.cache/pip/wheels/bc/00/68/b4cbf1017e55880ef2afd1a248a1c88311f38a574c8929d687
Successfully built psutil
Installing collected packages: psutil
Successfully installed psutil-5.3.1
1741164544
> docker run -m 800000000 -ti python:2.7 bash
root@b8a28ad93432:/# pip install psutil && python -c "import psutil; print psutil.virtual_memory().available"
Collecting psutil
Downloading psutil-5.3.1.tar.gz (397kB)
100% |████████████████████████████████| 399kB 2.8MB/s
Building wheels for collected packages: psutil
Running setup.py bdist_wheel for psutil ... done
Stored in directory: /root/.cache/pip/wheels/bc/00/68/b4cbf1017e55880ef2afd1a248a1c88311f38a574c8929d687
Successfully built psutil
Installing collected packages: psutil
Successfully installed psutil-5.3.1
1739603968
In first test, the job gets killed straight away. It is expected because of low RAM allocation.
But in second and third tests, even though the -m
parameters are 8 times difference, the python code returns the more or less same results: 1741164544 and 1739603968 (i.e. 1,741,164,544 and 1,739,603,968)
Why it is the case?
My host PC is a mac and it has 16G RAM.
Upvotes: 4
Views: 3511
Reputation: 11494
Docker uses CGroups to control resource usage by containers, so you should check CGroup files:
$ docker run -m 800000000 -ti centos bash
[root@c7406a25bc4b /]# cat /sys/fs/cgroup/memory/memory.limit_in_bytes
799997952
psutil
is likely uses /proc/meminfo
file which shows system's, not container, capabilities.
See also: https://serverfault.com/questions/680963/lxc-container-shows-hosts-full-ram-amount-and-cpu-count
Upvotes: 12