Self-Healing Docker Containers With Restart Policies

Docker Restart policies are the commands that you execute in order to change the settings of how Docker should behave when it is restarted. These commands are executed by using the Docker command-line tool.

As of writing this post Docker off four restart policies that can protect and mitigate different scenarios as shown below.

NameDescription
AlwaysRestart all the time and ignore exit code
NoDon’t restart the container automatically
On-failureRestart if exit code is not zero
unless-stoppedRestart always unless the container was stopped
Docker Restart Policies

Set Restart Policy

Now that we know all the restart options that we have, let’s go ahead and configure our container with a restart policy.

docker run --name deploy  -it --restart always  ubuntu 

Continuous Restart policy is the easiest one to change of all Docker Restart policies. You can change this Docker restart policy by using the following command: –restart=always. This will ensure that if any configuration errors are found, they will be corrected immediately. However, this Docker Restart policy will also drain your system’s resources.

Lastly, there is the Off Docker restart policy which ensures that after any configuration error or failure the container will not be restarted at all. This will ensure that your containerized applications are up and running. However, this also means that if there is a failure in the code of the service, it will not be restarted. To enable this policy you have to modify the previous command so that it becomes –restart=no.

Conclution

Docker Restart policies are used to guarantee that your applications continue to operate correctly after any configuration changes or failures. You may set different rules for each container and ensure that the appropriate one is utilized depending on your requirements.


Posted

in

by