Docker Restart Policy On-Failure|Start Docker Container Automatically

Docker Restart Policy On-Failure| Start Docker Container Automatically

There are scenarios when the server or the virtual machine stops running abruptly and needs an engineer to start the server. So, when the server is started and being back to normal, we don’t know whether the services are started on the server or not. If services are not started then we need to login into the machine manually and start the services/docker. in this tutorial i will explain how to restart docker containers automatically and docker restart policies.

In the second case, due to heavy load on the service or any runtime issues, services or the docker containers may get stopped and we are unaware about it until the issue arises in the live application. To overcome this issue docker provides us the restart policy option which we can pass during the docker container initiation.

Below shown below command ensures that if for any reason docker service gets stopped, it will restart the container no matter what.

sudo docker run --name nginx-live --restart=always -v /usr/share/nginx/html/:/usr/share/nginx/html/ -p 80:80  -d nginx:latest

“–name” tag means you can give the unique name to the container.

“–restart=always” specify the policy to restart the container always, whenever it is in a stopped or paused condition.

“-v ” specifies the volume mounting of the container and host system

“-p ” specifies the port binding functionality.

“-d ” specifies to run the container in daemon mode.

kinds of Docker Restart policies:

on-failure : It denotes restart container whenever it terminates with error and exit code a non-zero.
always :  – it denotes restart container always irrespective of manual stoppage or system error.
unless-stopped :   Similar to always, but it doesn’t restart if stopped manually
No :   Defines do not automatically restart containers.

That’s all you need to know about Docker restart policy.

 

Leave a Reply

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