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
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:
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.
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.
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:
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:
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.
0 Comments