Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check mem_limit within a docker container

After some crashes with a docker container with a too low mem_limit, how can i check in a container the mem_limit of this container? I want to print an error message on startup and exit if the mem_limit is set to low.

like image 748
Mandragor Avatar asked Feb 12 '17 11:02

Mandragor


People also ask

How do I see what's inside my Docker container?

In the latest versions of Docker, something like this is possible: docker exec <container> bash . So, you just open a shell inside the container. Similarly, you can do: docker exec <container> ls <dir path> and docker exec <container> cat <file path> . For bash however, add the -it options.

How can I see what process is running inside a container?

Like it was mentioned, if you are already inside of a container, then just use ps -eaf command to see the running processes.

How do I find out my containers memory limit?

The memory limit is enforced via cgroups. Therefore you need to use cgget to find out the memory limit of the given cgroup.

How can I check CPU usage in Docker?

We can get CPU usage of docker container with docker stats command. The column CPU % will give the percentage of the host's CPU the container is using.


1 Answers

The memory limit is enforced via cgroups. Therefore you need to use cgget to find out the memory limit of the given cgroup.

To test this you can run a container with a memory limit:

docker run --memory 512m --rm -it ubuntu bash

Run this within your container:

apt-get update
apt-get install cgroup-bin
cgget -n --values-only --variable memory.limit_in_bytes /
# will report 536870912

Docker 1.13 mounts the container's cgroup to /sys/fs/cgroup (this could change in future versions). You can check the limit using:

cat /sys/fs/cgroup/memory/memory.limit_in_bytes
like image 66
Sebastian Avatar answered Oct 05 '22 07:10

Sebastian