Need help?

Our experts have had an average response time of 13.14 minutes in February 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

How to auto-restart Docker containers after a host server crash

by | Dec 9, 2016

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.

Setup and manage your Docker system

GET IN TOUCH WITH THE DOCKER EXPERTS NOW!

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:

auto-restart docker containers

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.

 

auto-restart docker container policy

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’.

[ Are your spending too much time managing your Docker containers? Our Docker experts take care of your infrastructure and ensure its smooth functioning. ]

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.

 

auto-restart docker container and edit cpu share

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:

 

auto-restart docker containers change restart policy

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:

 

auto-restart docker containers restart limit

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.

[ Running a Docker infrastructure doesn’t have to be hard, or costly. Get world class Docker management services at affordable pricing. ]

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.

If you’d like to know how to manage your Docker system and to get the best out of them for your business, we’d be happy to talk to you.

 

Disk space issues in Docker ?

Wish you had more time to focus on your business? Let us help you.

Our engineers will assist you to setup, monitor and manage your Docker infrastructure 24/7.

GET IN TOUCH WITH THE EXPERTS NOW!

var google_conversion_label = "owonCMyG5nEQ0aD71QM";


Bobcares provides Outsourced Hosting Support and Outsourced Server Management for online businesses. Our services include Hosting Support Services, server support, help desk support, live chat support and phone support.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Categories

Tags

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF