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
[root@siddhesh ~]#


[root@siddhesh ~]# docker container run --restart no -dt nginx
[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
[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
[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
[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
[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.

bottom of page