⬆️ ⬇️

Docker and detection of available resources inside the container



How to explain to the docker container how much resources does it have?



Writing a translation of Java and Docker pushed me toward writing this small note : everyone and the lean on the search for information on the subject should know this . I have been using LXC for a long time, which also limits the resources of containers with cgroup tools, and this problem has already been solved there.





Docker provides usage statistics and limits in the container using cgroup tools that are accessible along the path (in Debian) /sys/fs/cgroup/ , but not all applications can work with it, including system utilities top, free, ps and so on. The Linux Containers project circumvented this problem with LXCFS , which provides the following files in the user environment:





If for LXC 2.x everything works out of the box, then docker needs to be taught this.

We will have a test subject:



 Debian jessie 4.6.0-0.bpo.1-amd64 docker Server Version: 1.12.2 lxcfs: 2.0.6-1~bpo8+1 


First of all, the Debian kernel needs to be allowed to regulate memory using cgroup, adding to the boot parameters:



 cgroup_enable=memory swapaccount=1 


And put lxcfs , which will start the corresponding service:



 # systemctl status lxcfs.service â—Ź lxcfs.service - FUSE filesystem for LXC 


As a result, the desired proc files will appear along the path /var/lib/lxcfs/ .

It remains to forward them inside the docker-container one by one. In the docker-compose format, the container file will look like this:



 CT_name: container_name: CT_name hostname: CT_name image: debian:stable mem_limit: 512m volumes: - /var/lib/lxcfs/proc/meminfo:/proc/meminfo - /var/lib/lxcfs/proc/uptime:/proc/uptime - /var/lib/lxcfs/proc/swaps:/proc/swaps - /var/lib/lxcfs/proc/cpuinfo:/proc/cpuinfo - /var/lib/lxcfs/proc/stat:/proc/stat - /var/lib/lxcfs/proc/diskstats:/proc/diskstats 


And check the result:



 # docker exec CT_name cat /proc/uptime 247.0 247.0 # docker exec CT_name free -m total used free shared buffers cached Mem: 512 11 500 4322 0 0 -/+ buffers/cache: 11 500 Swap: 512 0 512 


Let's run the test mentioned in the article "Java and Docker: Everybody Should Know" with the container parameters:



 CT_name: container_name: CT_name hostname: CT_name image: rafabene/java-container:openjdk mem_limit: 150m volumes: - /var/lib/lxcfs/proc/meminfo:/proc/meminfo 


 curl http://172.17.0.33:8080/api/memory Allocated more than 80% (73.5 MiB) of the Max allowed JVM memory size (73.5 MiB) 


where 172.17.0.33 is the ip issued to the container.

As you can see, the application has realized the issued memory limit.



Thanks for attention.



')

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



All Articles