{"id":239447,"date":"2021-04-30T16:00:24","date_gmt":"2021-04-30T13:00:24","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-docker-restart-policies-to-keep-containers-running-cloudsavvy-it\/"},"modified":"2021-04-30T16:00:24","modified_gmt":"2021-04-30T13:00:24","slug":"how-to-use-docker-restart-policies-to-keep-containers-running-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-restart-policies-to-keep-containers-running-cloudsavvy-it\/","title":{"rendered":"#How to Use Docker Restart Policies to Keep Containers Running \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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-6a30312b16779\" 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-6a30312b16779\" 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-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-restart-policies-to-keep-containers-running-cloudsavvy-it\/#Available_Policies\" >Available Policies<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-docker-restart-policies-to-keep-containers-running-cloudsavvy-it\/#Applying_a_Restart_Policy\" >Applying a Restart Policy<\/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-use-docker-restart-policies-to-keep-containers-running-cloudsavvy-it\/#Restart_Loops\" >Restart Loops<\/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-use-docker-restart-policies-to-keep-containers-running-cloudsavvy-it\/#Limiting_Restart_Retries\" >Limiting Restart Retries<\/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-use-docker-restart-policies-to-keep-containers-running-cloudsavvy-it\/#Investigating_Why_Containers_Stopped\" >Investigating Why Containers Stopped<\/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-use-docker-restart-policies-to-keep-containers-running-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use Docker Restart Policies to Keep Containers Running \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-10864\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Illustration showing the Docker logo\" width=\"1600\" height=\"900\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Docker gives you several options to manage your container\u2019s lifecycle. Containers do not normally restart automatically after they terminate. With restart policies, you can take control over individual container lifecycles.<\/p>\n<p>Restart policies will be used whenever a container stops running. Docker also looks at restart policies when the daemon starts up. You can use this mechanism to bring containers up with your host after reboots.<\/p>\n<h2 id=\"available-policies\"><span class=\"ez-toc-section\" id=\"Available_Policies\"><\/span>Available Policies<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are currently four different <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.docker.com\/config\/containers\/start-containers-automatically\">restart policies<\/a>:<\/p>\n<ul>\n<li><strong><code>no<\/code><\/strong> \u2013 This policy will never automatically start a container. This is the default policy for all containers created with <code>docker run<\/code>.<\/li>\n<li><strong><code>always<\/code><\/strong> \u2013 Docker will ensure the container is always running. If the container stops, it will be im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely restarted. You can still manually stop the container with <code>docker stop<\/code> but Docker will bring it back up next time the daemon restarts.<\/li>\n<li><strong><code>on-failure<\/code><\/strong> \u2013 The container will get restarted if it stops because of an error. Docker won\u2019t bring the container up after the daemon restarts.<\/li>\n<li><strong><code>unless-stopped<\/code><\/strong> \u2013 This functions similarly to <code>always<\/code>. The difference is that Docker won\u2019t ever restart the container if it has been manually stopped.<\/li>\n<\/ul>\n<p>You\u2019ll typically use one of the last three options for production workloads. As Docker containers are often used for long-running background services, you usually want them to restart whenever anything goes wrong. The <code>no<\/code> policy is best suited to local development use. It\u2019s also useful for utility containers which run a single executable and then terminate.<\/p>\n<p>It can be difficult to decide which restart policy to use. <code>always<\/code> is often the most natural choice but the daemon restart behaviour can easily be overlooked. If you want containers to reliably stay stopped after you\u2019ve run <code>docker stop<\/code>, you should use <code>unless-stopped<\/code>.<\/p>\n<p>Docker detects errors based on the exit code emitted from the container\u2019s foreground process. An exit code of <code>1<\/code> or higher is interpreted as an error. This matches the Unix handling of exit codes, where only <code>0<\/code> represents a successful execution.<\/p>\n<p>When Docker restarts your container, it\u2019s equivalent to running <code>docker run<\/code> again. That means the image\u2019s <code>ENTRYPOINT<\/code> <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\">script<\/a> will be invoked. Bootstrap systems should always be resilient to multiple invocations.<\/p>\n<h2 id=\"applying-a-restart-policy\"><span class=\"ez-toc-section\" id=\"Applying_a_Restart_Policy\"><\/span>Applying a Restart Policy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can start a container with a specific restart policy by passing the <code>--restart<\/code> flag to <code>docker run<\/code>:<\/p>\n<pre>docker run --name httpd --restart always httpd:latest<\/pre>\n<p>If you\u2019re using Docker Compose, add the <code>restart<\/code> field to your <code>docker-compose.yml<\/code>:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yml\">\n<pre class=\"de1\">services:&#13;\n  httpd:&#13;\n    image: httpd:latest&#13;\n    restart: always<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>You can change the restart policy of an existing container using <code>docker update<\/code>. Pass the name of the container to the command. You can find container names by running <code>docker ps -a<\/code>.<\/p>\n<pre>docker update --restart-policy unless-stopped httpd<\/pre>\n<p>You can use <code>docker update<\/code> with containers that are running or stopped.<\/p>\n<h2 id=\"restart-loops\"><span class=\"ez-toc-section\" id=\"Restart_Loops\"><\/span>Restart Loops<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Docker includes a couple of safeguards against perpetual restart loops. The first is a mandatory time delay before restart policies activate. Docker won\u2019t begin monitoring restarts until a container has been running for at least 10 seconds. This prevents a failed container from continually restarting.<\/p>\n<p>The other specialist behaviour concerns the <code>docker stop<\/code> command. Docker will always respect use of <code>docker stop<\/code>, so the container won\u2019t immediately restart after you run the command. If you actually want to restart the container, use <code>docker restart<\/code> instead.<\/p>\n<h2 id=\"limiting-restart-retries\"><span class=\"ez-toc-section\" id=\"Limiting_Restart_Retries\"><\/span>Limiting Restart Retries<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>on-failure<\/code> restart policy lets you specify how many retries should be attempted. Docker will give up and leave the container in a stopped state if it fails to start multiple times in succession.<\/p>\n<pre>docker run httpd:latest --restart on-failure:5<\/pre>\n<p>In this example, Docker will try to restart the container five times after a failure (non-zero exit code). If the container fails to start on the fifth attempt, no more retries will be attempted. This option is useful for containers where a persistent starting error is unlikely to be resolved without manual intervention.<\/p>\n<h2 id=\"investigating-why-containers-stopped\"><span class=\"ez-toc-section\" id=\"Investigating_Why_Containers_Stopped\"><\/span>Investigating Why Containers Stopped<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you need to know why a container stopped, run <code>docker ps -a<\/code>. This will show the details of all your containers, whether stopped or running. Find the target container and look in its \u201cStatus\u201d column. For stopped containers, the exit code will be shown in brackets. If the code is greater than zero, the container terminated due to an error.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10919\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/e5c169a4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1015\" height=\"72\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You\u2019d need to refer to the documentation for the process running in the container to determine the meanings of individual error codes. However, you can often get insights into what caused a crash by retrieving the container\u2019s logs. Logs remain available after a container is stopped.<\/p>\n<pre>docker logs my-container<\/pre>\n<p>The log stream aggregates the container\u2019s standard output and standard error streams. If the error\u2019s been logged, you should expect to see it in the last few lines of output.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Restart policies help ensure your Docker containers are there when you need them. The default <code>no<\/code> policy is unsuitable for most production workloads. You don\u2019t want your containers to stay stopped if they crash!<\/p>\n<p>Use of one of the three restart-capable policies makes your containers more resilient to hardware reboots and unexpected termination. Docker will maintain service availability in the event of a container crash.\n<\/p><\/div>\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.cloudsavvyit.com\/10912\/how-to-use-docker-restart-policies-to-keep-containers-running\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use Docker Restart Policies to Keep Containers Running \u2013 CloudSavvy IT&#8221; Docker gives you several options to manage your container\u2019s lifecycle. Containers do not normally restart automatically after they terminate. With restart policies, you can take control over individual container lifecycles. Restart policies will be used whenever a container stops running. Docker also&#8230;<\/p>\n","protected":false},"author":1,"featured_media":239448,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/04\/075c8694.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-239447","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\/239447","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=239447"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/239447\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/239448"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=239447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=239447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=239447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}