Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Limit and CPU Limit in Docker Container

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.

like image 548
pythonician_plus_plus Avatar asked Feb 21 '18 23:02

pythonician_plus_plus


People also ask

Do Docker containers have a memory limit?

The maximum amount of memory the container can use. If you set this option, the minimum allowed value is 6m (6 megabytes). That is, you must set the value to at least 6 megabytes.

How much CPU can a Docker container use?

On windows, a container defaults to using two CPUs. If hyperthreading is available this is one core and two logical processors. If hyperthreading is not available this is two cores and two logical processors. On linux, a container defaults to using all available CPUs of the host.

What happens when Docker container Hits memory limit?

The --memory parameter limits the container memory usage, and Docker will kill the container if the container tries to use more than the limited memory.


1 Answers

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

like image 89
Thol Voleak Avatar answered Sep 22 '22 05:09

Thol Voleak