Memory Limit and CPU Limit in Docker Container
Asked Answered
S

2

15

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.

Smyth answered 21/2, 2018 at 23:44 Comment(0)
S
4

UPDATE

This is the answer that works: https://mcmap.net/q/364638/-limit-memory-on-a-docker-container-doesn-39-t-work


I have just found this answer:

Limit memory on a docker container doesn't work

I am going to try that and come back with my observation.

Smyth answered 22/2, 2018 at 0:1 Comment(0)
S
6

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

Sideline answered 22/2, 2018 at 7:57 Comment(0)
S
4

UPDATE

This is the answer that works: https://mcmap.net/q/364638/-limit-memory-on-a-docker-container-doesn-39-t-work


I have just found this answer:

Limit memory on a docker container doesn't work

I am going to try that and come back with my observation.

Smyth answered 22/2, 2018 at 0:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.