Say you need to restart a VM or restart Docker. How do you restart a container?
E.g. you have:
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b431e9c92bae d5e863a4b712 "sleep 1d" 30 minutes ago Up 30 minutes frosty_shaw
then restarting Docker (e.g. on the Mac you can click the Docker icon in the toolbar and select Docker > Restart) you get:
docker ps Error response from daemon: Bad response from Docker engine
whilst Docker is restarting and:
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
after the restart (i.e. no containers).
You can use
docker container run --restart always -d <image id> sleep 1d
to restart after a docker reboot.
Restart policies are different from the
--live-restoreflag of the
See also https://docs.docker.com/config/containers/live-restore/#enable-live-restore
More on Restart Policies
There are 4 restart policies:
no is the default. i.e. don’t restart if a container stops.
The others are:
We’ve seen this before. E.g. let’s say we have a script:
#/bin/bash sleep 30 exit 1
exit 1 indicates an error (
exit 0 would indicate success).
which we use as follows:
FROM alpine ADD crash.sh /crash.sh CMD /bin/sh /crash.sh
We can build and run with:
docker build -t testing_restarts . docker container run -d testing_restarts
This will exit.
To restart with
always restart policy use:
docker container run --restart always -d testing_restarts
Now, when it crashes, under
docker ps you’ll see:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 18bac13c1f42 testing_restarts "/bin/sh -c '/bin/sh…" 31 seconds ago Restarting (1) Less than a second ago competent_shtern
Here we can restart a container if it exits with a non-zero exit code. We can also specify a number of retries. E.g.
docker container run --restart on-failure:3 -d testing_restarts
- the container will not restart if you do a
docker stop <container id>
- the container (and oddly even any containers that have stopped as a result of completing the
on-failurenumber of retries – although only the first time the daemon was restarted) WILL restart if you restart the docker daemon
Behaves the same as
always except if a container is stopped.
Note: if you manually stop a container its restart policy is ignored until the Docker daemon restarts.
Lets you keep containers alive when the daemon becomes unavailable,.
However, doing this on my installation of Docker gave:
time="2019-01-02T17:09:32.547843592Z" level=fatal msg="Error starting cluster component: --live-restore daemon configuration is incompatible with swarm mode"
because I was running a swarm service.
I had to restore to Factory Defaults which means signing in to cloud.docker.com again.