How can a Docker container tell whether its memory was limited? How can a Docker container tell whether its memory was limited? docker docker

How can a Docker container tell whether its memory was limited?


You could compare the total physical memory available to the number cgget gives you. If the number given by cgget is lower than the total physical memory, then you know for sure cgroups where used to limit the memory.

For instance if I run a container limiting the memory to 100M on my computer which has 2G of physical memory, cgget will report 104857600 while the free command reports 2098950144 bytes:

On the docker host :

# free -b             total       used       free     shared    buffers     cachedMem:    2098950144  585707520 1513242624     712704   60579840  367644672-/+ buffers/cache:  157483008 1941467136Swap:   3137335296          0 3137335296    

Start a container limited to 100M

docker run --rm -it --memory=100M <any-image-with-cgget-available> bash -l

Now within that container:

# free -b             total       used       free     shared    buffers     cachedMem:    2098950144  585707520 1513242624     712704   60579840  367644672-/+ buffers/cache:  157483008 1941467136Swap:   3137335296          0 3137335296    # cgget -n --values-only --variable memory.limit_in_bytes /104857600

Note that the free command will report the same values on the docker host as from within the containers.

In the end, the following bash script defines a function is_memory_limited that can be used in a test to check if cgroup where used to limit the memory.

#!/bin/bashset -eufunction is_memory_limited {    type free >/dev/null 2>&1 || { echo >&2 "The 'free' command is not installed. Aborting."; exit 1; }    type cgget >/dev/null 2>&1 || { echo >&2 "The 'cgget' command is not installed. Aborting."; exit 1; }    type awk >/dev/null 2>&1 || { echo >&2 "The 'awk' command is not installed. Aborting."; exit 1; }    local -ir PHYSICAL_MEM=$(free -m | awk 'NR==2{print$2}')    local -ir CGROUP_MEM=$(cgget -n --values-only --variable memory.limit_in_bytes / | awk '{printf "%d", $1/1024/1024 }')    if (($CGROUP_MEM <= $PHYSICAL_MEM)); then        return 0    else        return 1    fi}if is_memory_limited; then    echo "memory is limited by cgroup"else    echo "memory is NOT limited by cgroup"fi


Just my 2 cents, rather than installing any third-party tools, you can simply check the limited memory using docker stats

docker container run --name mytestserver -m 200M -dt nginx

As in this case, I limited the amount of memory to 200M, now to verify it

docker stats <container id>CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS3afb4a8cfeb7        mytestserver        0.00%               **1.387MiB / 200MiB**   0.69%               8.48MB / 24.8kB     39.2MB / 8.25MB     2