Skip to main content
RunBook Academy

← All labs in Docker & Containers

Lab · foundation · ~20 min

Lab 12: Restart policies in practice

B · Nested virtualisationC · Simulation

Objectives

  • Apply each restart policy and observe the behaviour
  • Restart the host and observe what comes back

Prerequisites

  • Lab 5: install Docker

Objective

Demonstrate how each restart policy behaves when the container exits and when the host reboots.

Tasks

Task 1: ‘always’ — restart even after manual stop

docker run -d --name pol-always --restart always alpine sh -c 'sleep 30; exit 1'
docker stop pol-always
# Wait 30s
docker ps -a --filter name=pol-always
# pol-always was restarted even though we stopped it

Task 2: ‘unless-stopped’ — respect manual stop

docker run -d --name pol-unless --restart unless-stopped alpine sh -c 'sleep 30; exit 1'
docker stop pol-unless
# Wait 30s
docker ps -a --filter name=pol-unless
# pol-unless stays in 'Exited' state; not restarted

Task 3: ‘on-failure’ — restart on non-zero exit

docker run -d --name pol-fail --restart on-failure alpine sh -c 'sleep 30; exit 1'
docker ps -a --filter name=pol-fail
# RestartCount keeps incrementing

docker run -d --name pol-ok --restart on-failure alpine sh -c 'sleep 30; exit 0'
# Wait 30s
docker ps -a --filter name=pol-ok
# pol-ok stays in 'Exited' state; exit 0 is success

Task 4: ‘no’ — no restart

docker run -d --name pol-no --restart no alpine sh -c 'sleep 30; exit 1'
# Wait 30s
docker ps -a --filter name=pol-no
# Exited, not restarted

Task 5: Simulate host reboot (skip in shared labs)

In a private lab only:

docker run -d --name reboot-test --restart always nginx:1.27
sudo reboot
# After reboot
docker ps --filter name=reboot-test
# 'always' container is back; 'no' / 'on-failure' may not be

Cleanup

docker rm -f pol-always pol-unless pol-fail pol-ok pol-no 2>/dev/null

Verification status

Last reviewed
2026-08-09
Executed end to end
not yet run on hardware

The commands and configuration here have been reviewed against the verified software versions, but nobody has run this lab start to finish on a system meeting its prerequisites. Treat the Expected Outcome as the intended result rather than an observed one, and keep the Cleanup section to hand.