How to restart containers automatically in docker
Tags: docker, linux, tutorial
Published: 2020-12-28 05:33:09

<!-- wp:paragraph -->
<p>If we are using docker containers for personal use we can restart it manually using docker restart. The docker restart command allow us to restart containers with some time limit. But, in production it is difficult to restart by manual. To restart containers automatically, we can use restart policies for docker containers.</p>
<!-- /wp:paragraph -->
<p>This post was originally posted on <a href="https://developersdoors.xyz/how-to-restart-containers-automatically-in-docker/">developersdoors</a></p>
<!-- wp:heading -->
<h2>Restart policies for docker containers</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Restart polies allows the container to restart automatically in required situations. The situations may be when a failure occurs, when daemon starts, when a container stopped.<br>The <code>--restart</code> flag is used with docker run command when starting a container. There are four restart policies available.</p>
<!-- /wp:paragraph -->

<!-- wp:table -->
<figure class="wp-block-table"><table><tbody><tr><td><code>no</code></td><td>Do not automatically restart containers anyway</td></tr><tr><td><code>always</code></td><td>Always restart the container even though the container stopped manually. Until the daemon stops or the restart policy is changed, it will restart it in loop.</td></tr><tr><td><code>on-failure</code></td><td>Restart the container if it exits due to an error, which manifests as a non-zero exit code. If any container is stopped due to any fault, it will automatically restart those containers with this policy.</td></tr><tr><td><code>unless-stopped</code></td><td>Similar to <code>always</code>, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.</td></tr></tbody></table></figure>
<!-- /wp:table -->

<!-- wp:paragraph -->
<p>To run a container with restart policies, try with following code pattern</p>
<!-- /wp:paragraph -->

<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: #f0f0f0; color: #444444;">docker run --restart no hello-world</pre>

<!-- wp:paragraph -->
<p>The above command run the <code>hello-world</code> image with restart policy set as <code>no</code>. It will not restart the containers automatically. </p>
<!-- /wp:paragraph -->

<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240); color: rgb(68, 68, 68);">docker update --restart always hello-world</pre>

<!-- wp:paragraph -->
<p>Here I use <code>update</code> command to update the restart policy of the hello-world container with <code>always</code> policy. This will restart the container always even though it is stopped manually or start when the daemon starts.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To view the events that occurs during restart we can use event command.</p>
<!-- /wp:paragraph -->

<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240); color: rgb(68, 68, 68);">docker event</pre>

<!-- wp:paragraph -->
<p>Open the docker event in one shell and run the container with always policy with another shell. The event shows the hello-world container will restart automatically every times it stopped. The image shows that every time the hello-world container stopped, it restart it.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1041,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="http://developersdoors.xyz/wp-content/uploads/2020/12/docker_events-693x1024.png" alt="docker restart containers(1)" class="wp-image-1041"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>We can also restart the containers using process managers such as upstart, systemd, supervisor. I will be posting an article on using process managers for docker in later. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For any reference to docker -&gt; <a href="https://docs.docker.com/">go</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For my other docker archives -&gt;<a href="https://developersdoors.xyz/category/programming/docker/"> go</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you like the article feel free to share.</p>
<!-- /wp:paragraph -->