Auto restart Docker containers after server crash

Auto restart Docker containers after server crash

Auto restart Docker containers after server crash: Docker containers can be used to host applications ranging from one-time testing to 24/7 web hosting instances. Depending on its purpose, the system setup can vary.

Every Docker machine runs a ‘docker’ daemon, which is required for the containers to function. But during instances such as server crash or reboot, this daemon may not get restarted.

Today we’ll see what is Docker restart policy and how to auto-restart Docker containers to ensure that there is no service downtime.

What is Docker restart policy?

To ensure that the Docker daemon gets restarted after a host machine restart, you can enable the docker service to start in machine boot.

But that’s not enough to guarantee your business uptime. Because, by default, the Docker containers will not restart when the docker daemon is restarted.

As a result, even if the host machine or the docker service restarts, the containers in it would be non-functional. This would cause downtime for the businesses hosted in them.

For services that require high uptime, such as website hosting, it is therefore important to auto-restart Docker containers after restarting the docker daemon.

This can be now set using the Docker restart policy. A restart policy controls whether the Docker daemon restarts a container after exit.

Using the restart policy, when the docker daemon restarts, the containers will be restarted too. The default policy is ‘no’, as we discussed.

Docker supports the following restart policies for its containers and can be set during container start:

                                                                            Container restart policies

How to auto-restart Docker containers

To set a restart policy for a docker container, you can start the container using ‘docker run’ and with the parameter ‘–restart‘.

To auto-restart the containers whenever they go down, use the command with the restart policy ‘always‘ as shown. Whenever the container exits, the docker daemon would restart it.

Specify restart policy during container start

When a restart policy is active on a container, its ‘STATUS‘ will be shown as either ‘Up’or ‘Restarting’ when we list the docker containers using ‘docker ps’.

How to change a running container’s restart policy

Suppose you have a running container and you need to change its settings or configuration on-the-go, without incurring a downtime by restarting it.

You can do that using the command ‘docker update’ container. The ‘docker update’ command dynamically updates container resources such as CPU and I/O.

This is mostly helpful in the case of resource management, where you want to restrict a container or many containers from taking up more CPU.

Edit CPU shares of container with ‘docker update

For instance, the above command limits the CPU share for the running container to 512, as we can verify using ‘docker inspect’ command.

To change the restart policy of a container during run-time, use the ‘docker update’ container command with the ‘–restart’ option:

Change restart policy using ‘docker update’ container

The ‘on-failure‘ option for ‘restart’ will set the number of ‘restart’ attempts on the container to 3. Otherwise Docker will keep on trying to restart the container forever and waste the resources.

Once set with ‘docker update’ command, the restart limit for the container can be verified using ‘docker inspect’ command:

Restart limits of container

With this on-failure setting, when the container does not exit successfully, it will be attempted to restart, but the number of restart attempts would be limited to 3.

This retry limit helps to save time and resources of the Docker system by not attempting to restart containers whose applications have exited successfully.

Points to note..

Today we saw how to set auto-restart Docker containers by setting restart policy using ‘docker update’, so as to ensure that your container is running even after the host machine reboots.

Before selecting a restart policy for a container, it is important to consider its business purpose, priority, host machine resources and the container workload.

Was this Tutorial helpful? Help others share on Facebook, Twitter, and Google Plus!

Enjoyed this video?
Auto restart Docker containers after server crash
"No Thanks. Please Close This Box!"