π³ Day 12: Docker Restart Policy β Master It Like a Pro! π

Table of contents
- π¬ Video Demonstration
- π What is a Docker Restart Policy?
- π Available Restart Policies
- π Docker Restart Policies (Deep Dive)
- π§ How to Set Restart Policy
- π― Practice Examples β Letβs Try Them Out!
- π Containerβs Restart Policy in Production(Check and update Policy)
- π§Ό Clean Up (After Practice)
- Choosing the Right Restart Policy
- β Best Practices
- π Additional Resources

Welcome to this video on Docker Restart Policies! Whether you're a Docker newbie or an intermediate user, this guide will help you understand, practice, and master restart policies to build resilient containers. Letβs dive in! π‘
π¬ Video Demonstration
π What is a Docker Restart Policy?
Docker restart policies help you automatically restart containers under certain conditions (like after failure, reboot, etc.) β useful for keeping your services up without manual intervention. Understanding Docker restart policies is crucial for maintaining container uptime and ensuring that services recover from failures automatically.
π Available Restart Policies
Policy | Description |
no | π Default. Never restart the container automatically. |
always | π Always restart the container if it stops. |
unless-stopped | βοΈ Restart the container unless it's explicitly stopped by the user. |
on-failure | β Restart the container only if it exits with a non-zero (failure) status code. You can limit restart attempts. |
π Docker Restart Policies (Deep Dive)
Restart Policy | docker stop | docker kill | App crash (exit ) | Host reboot | Daemon restart |
no | β | β | β | β | β |
always | β | β | β | β | β |
unless-stopped | β | β | β | β * | β * |
on-failure | β | β | β ** | β | β |
Note: For unless-stopped: β
* It restarts only if it was running before the host reboot or daemon restart.
If you stopped the container manually (docker stop
), it will not restart on reboot or daemon restart. on-failure, β
** indicates the container restarts only if the app exits with a non-zero exit code.
π§ How to Set Restart Policy
You can set the restart policy using the --restart
flag while running a container:
docker run --restart=<policy> [OPTIONS] IMAGE [COMMAND] [ARG...]
π― Practice Examples β Letβs Try Them Out!
1. π« no
β Default Behavior (No Restart)
This policy is the default setting. With the no
restart policy, Docker does nothing if a container stops or crashes.
# Run container
docker run -itd --name no-restart ibraransaridocker/restart-policy-demo
# Above command is similar to below command
docker run -itd --name no-restart --restart=no ibraransaridocker/restart-policy-demo
2. π always
β Restart Forever
When you set a containerβs restart policy to always
, Docker attempts to restart the container every time it stops, or If it is manually stopped, regardless of the reason. it is restarted only when Docker daemon restarts.
# π Stop it and watch it restart:
docker run -itd --name always --restart=always ibraransaridocker/restart-policy-demo
docker stop always
docker ps -a | grep always
docker rm -f always
# π Kill it and watch it restart:
docker run -itd --name always --restart=always ibraransaridocker/restart-policy-demo
docker kill always
docker ps -a | grep always
docker rm -f always
# π App crash (`exit`) and watch it restart:
docker run -itd --name always --restart=always ibraransaridocker/restart-policy-demo
docker exec always kill 1
docker inspect always --format='{{.RestartCount}}'
docker ps -a | grep always
docker rm -f always
# π Daemon Restart
docker run -itd --name always --restart=always ibraransaridocker/restart-policy-demo
docker ps -a | grep always
sudo systemctl restart docker
docker ps -a | grep always
docker rm -f always
# π Even after reboot:
docker run -itd --name always --restart=always ibraransaridocker/restart-policy-demo
docker ps -a | grep always
sudo init 6
docker ps -a | grep always
docker rm -f always
3. βοΈ unless-stopped
β Persistent Until You Stop It
The unless-stopped
policy tells Docker to keep restarting the container until a developer forcibly stops it. It's similar to always
, but it respects intentional stops. it is not restarted even after Docker daemon restarts.
# Run Container
docker run -itd --name unless --restart=unless-stopped ibraransaridocker/restart-policy-demo
# βοΈ This will restart unless you explicitly stop it with:
docker stop test-unless
Then restart your system β it wonβt restart again unless you start it manually.
4. β on-failure
β Conditional Restart Based on Exit Code
The on-failure
policy instructs Docker to restart the container only if it exits with an error code (non-zero exit status). Restart the container if it exits due to an error.
# Run container
docker run -itd --name failure --restart=always ibraransaridocker/restart-policy-demo
# π App crash (`exit`) and watch it restart:
docker exec failure kill 1
docker ps -a | grep failure
# π It will try to restart 3 times, then stop.
Check restart count:
docker inspect test-failure --format='{{.RestartCount}}'
π Containerβs Restart Policy in Production(Check and update Policy)
# You can inspect any containerβs restart policy with:
docker inspect <container_name> --format='{{.HostConfig.RestartPolicy.Name}}'
# Check all containers restart policy
docker inspect --format '{{.Name}} Restart: {{ .HostConfig.RestartPolicy.Name }}' $(docker ps -aq)
# Reset restart single container
docker update --restart=no <container_name_or_id>
# Reset restart All container
docker update --restart=no $(docker ps -q)
# Set all restart always
docker update --restart=always $(docker ps -aq)
π§Ό Clean Up (After Practice)
docker rm -f $(docker ps -aq)
Choosing the Right Restart Policy
Here are some considerations for choosing the right restart policy:
Development vs. Production: In a development environment, you might prefer
no
to debug issues, while in production,always
oron-failure
could be more appropriate.Stateless vs. Stateful Containers: Stateless containers can often use
always
, but stateful containers might need a more nuanced approach likeon-failure
.Dependent Services: For containers that depend on other services,
on-failure
ensures they don't restart in a loop if a dependency is unavailable.
β Best Practices
Use
always
for critical services that should never go down.Use
unless-stopped
for services you want to auto-restart, but still be able to manually shut down.Use
on-failure
for short-lived jobs or scripts that may fail and need retry.Avoid
no
unless you're doing one-off or testing containers.
π Additional Resources
- π Docker Policy
Subscribe to my newsletter
Read articles from Ibrar Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ibrar Ansari
Ibrar Ansari
Hello! I'm a DevOps Engineer dedicated to continuous learning and contributing to the tech community. If you find value in what I share, feel free to spread the word to others who might benefit as well. Your support is greatly appreciated!