Learn what restart policies do and how to use them
Make sure Docker is installed on your system and the service is started
# Fedora/RHEL/CentOS
rpm -qa | grep docker
systemctl status docker
docker run --restart always --name forest alpine sleep 15
docker container ls
- Is the container running? What about after 15 seconds, is it still running? why?It runs even after it completes to run sleep 15
because the restart policy is “always”. This means that Docker will keep restarting the same container even after it exists.
The restart policy doesn’t apply when the container is stopped with the command docker container stop
docker container stop forest
docker container rm forest
sleep 600
and verify it runsdocker run --restart always --name forest alpine sleep 600
docker container ls
sudo systemctl restart docker
Yes, it’s still running due to the restart policy always
which means Docker will always bring up the container after it exists or stopped (not with the stop command).
unless-stopped
docker update --restart unless-stopped forest
docker container stop forest
sudo systemctl restart docker
No, the container is not running. This is because we changed the policy to unless-stopped
which will run the container unless it was in stopped status. Since before the restart we stopped the container, Docker didn’t continue running it after the restart.