Skip to content

Limit docker container memory usage

We should limit the memory usage to the containers like limiting the CPU usage. Providing unlimited memory usage to the docker containers may lead to Out of memory fatal errors and might result in performance issues. Hence, it’s important to restrict memory usage of docker containers. To avoid this we can set define how much RAM the docker container can utilize.

These memory values are positive integers suffixed with b,k,m and g to represent bytes, kilobytes, megabytes and gigabytes.


Allocating maximum memory

To allocation the maximum memory a container can use, we can use the option –m or –memory. The minimum value is 4m.

docker run -it --memory="1g" ubuntu

Allocating memory swap to disk limit

To amount of memory this container is allowed to swap to disk, we use –memory-swap option.

docker run -it --memory="1g" --memory-swap="2g" ubuntu

Allocating kernel memory

We can specify the maximum amount of kernel memory the container can use using –kernel-memory option. Default value is 4m.

docker run -it --oom-kill-disable ubuntu

Disabling out of memory kill

When out-of-memory (OOM) fatal error occurs, the kernel kills the other processes in the container to keep the system alive. To disable this option we can use –oom-kill-disable option.

docker run -it --oom-kill-disable ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.