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
- Run a container with the following properties:
- image: alpine
- name: forest
- restart policy: always
- command to execute: sleep 15
docker run --restart always --name forest alpine sleep 15
- Run
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.
- How then can we stop the container from running?
The restart policy doesn't apply when the container is stopped with the command docker container stop
- Remove the container you've created
docker container stop forest
docker container rm forest
- Run the same container again but this time with
sleep 600
and verify it runs
docker run --restart always --name forest alpine sleep 600
docker container ls
- Restart the Docker service. Is the container still running? why?
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).
- Update the policy to
unless-stopped
docker update --restart unless-stopped forest
- Stop the container
docker container stop forest
- Restart the Docker service. Is the container running? why?
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.