top of page

Docker Restart Policy


Docker Restart Policy

In Docker, the restart policy is a setting that determines what action Docker should take if a container exits. You can specify a restart policy when running a container to control its behaviour in case of failure or manual termination. The restart policy is specified using the --restart flag when running the docker run command.


 

Here are some common restart policies:


1.No This is the default restart policy. If the container exits, it will not be restarted.

[root@siddhesh ~]# docker container run -dt nginx
82b930c9cb30ee1c641467c42a03c9539e7dcaaa442055086d2dca59c4e946a7
[root@siddhesh ~]#

OR

[root@siddhesh ~]# docker container run --restart no -dt nginx
82b930c9cb30ee1c641467c42a03c9539e7dcaaa442055086d2dca59c4e946a7
[root@siddhesh ~]#

Verify :

[root@siddhesh ~]# docker inspect -f "{{ .HostConfig.RestartPolicy }}" 82b930c9cb30
{no 0}
[root@siddhesh ~]#

2. Always


Docker will always restart the container regardless of the exit status.

[root@siddhesh ~]# docker container run --restart always -dt nginx
ebcdf60105469fde5366674baff4b39356aa6ba8de5c351a11b11cd4383478d4
[root@siddhesh ~]#  docker inspect -f "{{ .HostConfig.RestartPolicy }}" ebcdf6010546
{always 0}
[root@siddhesh ~]#

3. Unless-stopped


The container will restart unless it is explicitly stopped by the user.

[root@siddhesh ~]# docker container run --restart unless-stopped -dt nginx
5da46ff11a9e009ab2755d92ebfa2d90c00db07667580a76968667e044a3a29a
[root@siddhesh ~]# docker inspect -f "{{ .HostConfig.RestartPolicy }}" 5da46ff11a9e
{unless-stopped 0}
[root@siddhesh ~]#

4. On-failure The container will restart only if it exits with a non-zero status, indicating a failure.

[root@siddhesh ~]# docker container run --restart on-failure -dt nginx
cb0cc58727e61b453192d08ec16690d38efae57f1677d614c54b3c3b1021d355
[root@siddhesh ~]# docker inspect -f "{{ .HostConfig.RestartPolicy }}" cb0cc58727e6
{on-failure 0}
[root@siddhesh ~]#

You can also specify an optional maximum retry count:

[root@siddhesh ~]# docker container run --restart on-failure:3 -dt nginx
f868934614f0eaa336ef52d3d362953c820c5880a180dc15e92ef5c482fa22b4
[root@siddhesh ~]# docker inspect -f "{{ .HostConfig.RestartPolicy }}" f868934614f0
{on-failure 3}
[root@siddhesh ~]#

In this example will attempt to restart the container up to 3 times if it fails.


These restart policies help ensure the availability of services within Docker containers. Depending on your use case, you may choose the appropriate restart policy to meet your application's requirements.


Keep the following in mind when using restart policies:

  1. A restart policy only takes effect after a container starts successfully. In this case, starting successfully means that the container is up for at least 10 seconds and Docker has started monitoring it. This prevents a container which doesn't start at all from going into a restart loop.

  2. If you manually stop a container, the restart policy is ignored until the Docker daemon restarts or the container is manually restarted. This prevents a restart loop.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page