Skip to content

Docker Deep Dive: Having Containers Start Automatically

Tanveer Alam edited this page Aug 9, 2019 · 3 revisions

Having Containers Start Automatically

Restart flag and how to use it and set the restart policies for a container.

By default, restart is set to no. So if docker deamon stops or the server restarts, those containers are not going to start back up.

Auto-restarting

To configure the restart policy for a container, use the --restart flag:

  • no: The default. Do not automatically restart the container.
  • 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.
  • unless-stopped: Similar to always, except that when the container is stopped, it is not restarted even after the Docker daemon restarts.

docker container run -d --name <NAME> --restart <RESTART> <IMAGE>

Example:

$ docker container run -d --name always-restart --restart always on2411/weather-app:latest
41f8b94ff018f947a3fd06accbc57ca2a053d458c185443552df038ee4736584

$ docker container run -d --name unless-restart --restart unless-stopped on2411/weather-app:latest
74294fcb554a62fa18489112c381bda9914f106a2ee9c0ebf12ff17f42dd2c2b

$ docker container ls
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS               NAMES
74294fcb554a        on2411/weather-app:latest   "./bin/www"         25 seconds ago      Up 21 seconds       3000/tcp            unless-restart
41f8b94ff018        on2411/weather-app:latest   "./bin/www"         55 seconds ago      Up 51 seconds       3000/tcp            always-restart
db38f22331c9        centos                      "/bin/bash"         2 hours ago         Up About an hour                        container_process

Restart docker daemon: You will see container_process container is stopped

$ sudo systemctl restart docker

$ docker container ls
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS               NAMES
74294fcb554a        on2411/weather-app:latest   "./bin/www"         2 minutes ago       Up 14 seconds       3000/tcp            unless-restart
41f8b94ff018        on2411/weather-app:latest   "./bin/www"         2 minutes ago       Up 14 seconds       3000/tcp            always-restart


$ docker container ls -a
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS                        PORTS               NAMES
74294fcb554a        on2411/weather-app:latest   "./bin/www"         2 minutes ago       Up 47 seconds                 3000/tcp            unless-restart
41f8b94ff018        on2411/weather-app:latest   "./bin/www"         3 minutes ago       Up 47 seconds                 3000/tcp            always-restart
db38f22331c9        centos                      "/bin/bash"         2 hours ago         Exited (137) 59 seconds ago                       container_process

Now stop the unless-restart container and restart the docker daemon:

$ docker container stop unless-restart
unless-restart

$ docker container ls
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS               NAMES
41f8b94ff018        on2411/weather-app:latest   "./bin/www"         5 minutes ago       Up 2 minutes        3000/tcp            always-restart
$ sudo systemctl restart docker

$ docker container ls
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS               NAMES
41f8b94ff018        on2411/weather-app:latest   "./bin/www"         6 minutes ago       Up 24 seconds       3000/tcp            always-restart

Now stop the always-restart container and restart docker daemon.

$ docker container stop always-restart
always-restart

$ sudo systemctl restart docker

$ docker container ls
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS              PORTS               NAMES
41f8b94ff018        on2411/weather-app:latest   "./bin/www"         8 minutes ago       Up 31 seconds       3000/tcp            always-restart

Container's World



Clone this wiki locally