Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker - what does `docker run --restart always` actually do?

Tags:

docker

Although it seems like the --restart flag is simple and straightforward, I came up with a number of questions when experimenting with it:

  1. With respect to ENTRYPOINT definitions - what are the actual defined semantics during restart?
  2. If I exec into the container (I am on a DDC) and kill -9 the process, it restarts, but if I do docker kill it does not. Why?
  3. How does restart interact with Shared Data Containers / Named Volumes?
like image 355
JoeG Avatar asked Jan 09 '17 19:01

JoeG


People also ask

What does docker restart always do?

Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. Restart policies ensure that linked containers are started in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers.

What does restart always mean in docker compose?

docker-compose down removes the container within seconds. If you use docker-compose up to start up a container, use docker-compose down to take it down. restart: always restarts containers that exit with zero ( success ) exit code.

What does docker run actually do?

The docker run command creates running containers from images and can run commands inside them. When using the docker run command, a container can run a default action (if it has one), a user specified action, or a shell to be used interactively.

Does restarting docker restart all containers?

For a major version upgrade, one has to tear down all running containers. With a restart policy of always , however, the containers will be restarted after the docker daemon is restarted after the upgrade.


2 Answers

Restart policies

Using the --restart flag on Docker run you can specify a restart policy for how a container should or should not be restarted on exit.

When a restart policy is active on a container, it will be shown as either Up or Restarting in docker ps. It can also be useful to use docker events to see the restart policy in effect.

docker run --always  

Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.

I recommend you this documentation about restart-policies

Documentation - Restart policies

Update Docker v19.03

Restart policies (--restart)

Use Docker’s --restart to specify a container’s restart policy. A restart policy > controls whether the Docker daemon restarts a container after exit. Docker supports the following restart policies:

always Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.

$ docker run --restart=always redis 

Documentation - Restart policies

like image 86
Harold Castillo Avatar answered Sep 23 '22 00:09

Harold Castillo


To configure the restart policy for a container, use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following:

no Do not automatically restart the container. (the default)

on-failure Restart the container if it exits due to an error, which manifests as a non-zero exit code.

always Always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted.

unless-stopped Similar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.

The following example starts a Redis container and configures it to always restart unless it is explicitly stopped or Docker is restarted.

$ docker run -d --restart unless-stopped redis 

This command changes the restart policy for an already running container named redis.

$ docker update --restart unless-stopped redis 

And this command will ensure all currently running containers will be restarted unless stopped.

$ docker update --restart unless-stopped $(docker ps -q) 

Restart policy details

Keep the following in mind when using restart policies:

  • 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 does not start at all from going into a restart loop.
  • If you manually stop a container, its restart policy is ignored until the Docker daemon restarts or the container is manually restarted. This is another attempt to prevent a restart loop.
  • Restart policies only apply to containers. Restart policies for swarm services are configured differently.

Documentation

like image 45
Charith Jayasanka Avatar answered Sep 23 '22 00:09

Charith Jayasanka