Memory Limit and CPU Limit in Docker Container

pythonician_plus_plus picture pythonician_plus_plus · Feb 22, 2018 · Viewed 30k times · Source

UPDATE:

found a lot of questions and discussions on stackoverflow about this topic. And although they are marked as accepted answered and started by thousands of users, they do not seem to be the right answer here.


I ran a docker (version 1.13.1, build 092cba3) container with resource constraints as follows:

docker run --privileged -v /sys/fs/cgroup:/sys/fs/cgroup -m 4096M --cpuset-cpus='0' --cpus=1 --cpu-shares=256 -p $IMAGE_NAME

The host system (RHEL 7) has 4 cores and 8G memory. Basically I want to restrict the available memory and CPU for the container. After successfully launched the container, I opened the bash and tried to find the limit information from within the container. But I wasn't able to get the correct information.

I tried this:

sudo cat /proc/meminfo

The result is:

Host system MemTotal: 8008812 kB MemFree: 7416404 kB MemAvailable: 7537332 kB
Docker Container MemTotal: 8008812 kB MemFree: 7318052 kB MemAvailable: 7498764 kB

Similarly, I wanted to get CPU limit:

grep -c ^processor /proc/cpuinfo

The result is:

Host system 4
Docker Image 4

It seems that the CPU and memory limit enforced by container is not visible by the container. I also tried to query cgroup information.

mkdir -p /tmp/memory
mount -n -t cgroup -o memory cgroup /tmp/memory

Then I look into the cgroup files:

[root@engrlab memory]# cat /tmp/memory/memory.limit_in_bytes
9223372036854771712

This number is greater than the actual memory of the host system.

Is there a way to verify that the resource constraints has been correctly set in container? How do I find the resource constraint info from within a container? Appreciate any suggestion.

Answer

Thol Voleak picture Thol Voleak · Feb 22, 2018

I try this command this my docker host which is working

docker run -it -d -m 100M --cpuset-cpus='0' --cpus=1 --cpu-shares=256 --name testing ubuntu /bin/bash

docker stats testing