{"id":488083,"date":"2022-08-27T03:48:26","date_gmt":"2022-08-27T00:48:26","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-set-a-memory-limit-for-docker-containers\/"},"modified":"2022-08-27T03:48:26","modified_gmt":"2022-08-27T00:48:26","slug":"how-to-set-a-memory-limit-for-docker-containers","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-set-a-memory-limit-for-docker-containers\/","title":{"rendered":"#How to Set a Memory Limit for Docker Containers"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a29751759a2e\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a29751759a2e\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-set-a-memory-limit-for-docker-containers\/#%E2%80%9CHow_to_Set_a_Memory_Limit_for_Docker_Containers%E2%80%9D\" >&#8220;How to Set a Memory Limit for Docker Containers&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-set-a-memory-limit-for-docker-containers\/#How_Docker_Memory_Limits_Work\" >How Docker Memory Limits Work<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-set-a-memory-limit-for-docker-containers\/#Setting_Hard_and_Soft_Memory_Limits\" >Setting Hard and Soft Memory Limits<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-set-a-memory-limit-for-docker-containers\/#Managing_Swap_Memory\" >Managing Swap Memory<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-set-a-memory-limit-for-docker-containers\/#Disabling_Out-of-Memory_Process_Kills\" >Disabling Out-of-Memory Process Kills<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-set-a-memory-limit-for-docker-containers\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Set_a_Memory_Limit_for_Docker_Containers%E2%80%9D\"><\/span>&#8220;How to Set a Memory Limit for Docker Containers&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-805981\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Docker-New.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Docker logo\" width=\"1600\" height=\"900\"\/><\/p>\n<p>Docker containers <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/containers\/resource_constraints\">default to running without<\/a> any resource constraints. Processes running in containers are free to utilize limitless amounts of memory, potentially impacting neighboring containers and other workloads on your host.<\/p>\n<p>This is hazardous in production environments. Each container should be configured with an <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ropriate memory limit to prevent runaway resource consumption. This helps reduce contention which will maximize overall system stability.<\/p>\n<h2 id=\"how-docker-memory-limits-work\"><span class=\"ez-toc-section\" id=\"How_Docker_Memory_Limits_Work\"><\/span>How Docker Memory Limits Work<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker lets you set hard and soft memory limits on individual containers. These have different effects on the amount of available memory and the behavior when the limit is reached.<\/p>\n<ul>\n<li><strong>Hard memory limits<\/strong> set an absolute cap on the memory provided to the container. Exceeding this limit will normally cause the kernel out-of-memory killer to terminate the container process.<\/li>\n<li><strong>Soft memory limits<\/strong> indicate the amount of memory a container\u2019s expected to use. The container is permitted to use more memory when capacity is available. It could be terminated if it\u2019s exceeding a soft limit during a low-memory condition.<\/li>\n<\/ul>\n<p>Docker also provides controls for setting swap memory constraints and changing what happens when a memory limit is reached. You\u2019ll see how to use these in the following sections.<\/p>\n<h2 id=\"setting-hard-and-soft-memory-limits\"><span class=\"ez-toc-section\" id=\"Setting_Hard_and_Soft_Memory_Limits\"><\/span>Setting Hard and Soft Memory Limits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A hard memory limit is set by the <code>docker run<\/code> command\u2019s <code>-m<\/code> or <code>--memory<\/code> flag. It takes a value such as <code>512m<\/code> (for megabytes) or <code>2g<\/code> (for gigabytes):<\/p>\n<pre>$ docker run --memory=512m my-app:latest<\/pre>\n<p>Containers have a minimum memory requirement of 6MB. Trying to use <code>--memory<\/code> values less than <code>6m<\/code> will cause an error.<\/p>\n<p>Soft memory limits are set with the <code>--memory-reservation<\/code> flag. This value needs to be lower than <code>--memory<\/code>. The limit will only be enforced when container resource contention occurs or the host is low on physical memory.<\/p>\n<pre>$ docker run --memory=512m --memory-reservation=256m my-app:latest<\/pre>\n<p>This example starts a container which has 256MB of reserved memory. The process could be terminated if it\u2019s using 300MB and capacity is running out. It will always stop if usage exceeds 512MB.<\/p>\n<h2 id=\"managing-swap-memory\"><span class=\"ez-toc-section\" id=\"Managing_Swap_Memory\"><\/span>Managing Swap Memory<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Containers can be allocated swap memory to accommodate high usage without impacting physical memory consumption. Swap allows the contents of memory to be written to disk once the available RAM has been depleted.<\/p>\n<p>The <code>--memory-swap<\/code> flag controls the amount of swap space available. It only works in conjunction with <code>--memory<\/code>. When you set <code>--memory<\/code> and <code>--memory-swap<\/code> to different values, the swap value controls the total amount of memory available to the container, including swap space. The value of <code>--memory<\/code> determines the portion of the amount that\u2019s physical memory.<\/p>\n<pre>$ docker run --memory=512m --memory-swap=762m my-app:latest<\/pre>\n<p>This container has access to 762MB of memory of which 512MB is physical RAM. The remaining 250MB is swap space stored on disk.<\/p>\n<p>Setting <code>--memory<\/code> without <code>--memory-swap<\/code> gives the container access to the same amount of swap space as physical memory:<\/p>\n<pre>$ docker run --memory=512m my-app:latest<\/pre>\n<p>This container has a total of 1024MB of memory, comprising 512MB of RAM and 512MB of swap.<\/p>\n<p>Swap can be disabled for a container by setting the <code>--memory-swap<\/code> flag to the same value as <code>--memory<\/code>. As <code>--memory-swap<\/code> sets the total amount of memory, and <code>--memory<\/code> allocates the physical memory proportion, you\u2019re instructing Docker that 100% of the available memory should be RAM.<\/p>\n<p>In all cases swap only works when it\u2019s enabled on your host. Swap reporting inside containers is unreliable and shouldn\u2019t be used. Commands such as <code>free<\/code> that are executed within a container will display the total amount of swap space on your Docker host, not the swap accessible to the container.<\/p>\n<h2 id=\"disabling-out-of-memory-process-kills\"><span class=\"ez-toc-section\" id=\"Disabling_Out-of-Memory_Process_Kills\"><\/span>Disabling Out-of-Memory Process Kills<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Out-of-memory errors in a container normally cause the kernel to kill the process. This results in the container stopping with exit code 137.<\/p>\n<p>Including the optional flag <code>--oom-kill-disable<\/code> with your <code>docker run<\/code> command disables this behavior. Instead of stopping the process, the kernel will simply block new memory allocations. The process will appear to hang until you either reduce its memory use, cancel new memory allocations, or manually restart the container.<\/p>\n<p>This flag shouldn\u2019t be used unless you\u2019ve implemented mechanisms for resolving out-of-memory conditions yourself. It\u2019s usually better to let the kernel kill the process, causing a container restart that restores normal memory consumption.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker containers come without pre-applied resource constraints. This leaves container processes free to consume unlimited memory, threatening the stability of your host.<\/p>\n<p>In this article you\u2019ve learned how to set hard and soft container memory limits to reduce the chance you\u2019ll hit an out-of-memory situation. Setting these limits across all your containers will reduce resource contention and help you stay within your host\u2019s physical memory capacity. You should consider <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/containers\/resource_constraints\">using CPU limits<\/a> alongside your memory caps \u2013 these will prevent individual containers with a high CPU demand from detrimentally impacting their neighbors.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/devops\/how-to-set-a-memory-limit-for-docker-containers\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Set a Memory Limit for Docker Containers&#8221; Docker containers default to running without any resource constraints. Processes running in containers are free to utilize limitless amounts of memory, potentially impacting neighboring containers and other workloads on your host. This is hazardous in production environments. Each container should be configured with an appropriate memory&#8230;<\/p>\n","protected":false},"author":1,"featured_media":488084,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2022\/05\/Docker-New.jpeg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-488083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/488083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=488083"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/488083\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/488084"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=488083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=488083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=488083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}