Docker - Implementing Container Memory Limits

If you are running multiple containers on a single host, the one may want to implement memory limits on the containers to reduce the likelyhood that any single container having a memory meltdown and causing issues on the host. Here's how.

This tutorial was tested on an Ubuntu 14.04 64bit hardware virtualized t2 micro instance in Amazon Web Services

Steps

One can implement memory limits on containers by simply adding the following option when starting the container:

-m="$number$optional unit"
Unit can be = b (bytes), k(kilobytes), m(megabytes) or g(gigabytes).

However, you are likely to get the following error message when you do so for the first time:

WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.

This can be resolved by getting your kernel to allow memory accounting with the following commands. Its probably easier to just execute them as a script.

SEARCH='GRUB_CMDLINE_LINUX=""'
REPLACE='GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"'
FILEPATH="/etc/default/grub"
sudo sed -i "s;$SEARCH;$REPLACE;" $FILEPATH
sudo update-grub
echo "You now need to reboot for the changes to take effect"
After executing the script you will need to reboot the computer. You only have to perform this configuration once.

References

No comments:

Post a Comment