The --restart flag for Docker containers is used to define the container's restart policy. The two options, --restart=unless-stopped and --restart=always, have some subtle but important differences:
--restart=always:
- The container will always try to restart, no matter how it stopped.
- If the container crashes, it automatically restarts.
- If the Docker daemon is restarted, the container will also start automatically.
- Even if the container is stopped manually (as in using the docker stop command), it will restart when the Docker daemon restarts.
--restart=unless-stopped:
- The container automatically restarts when it crashes.
- If the Docker daemon is restarted, the container will also start automatically.
- Key difference: If a container is manually stopped (e.g., using the docker stop command), it will not start automatically when the Docker daemon restarts.
Summary of the main differences:
The always policy attempts to restart the container in all cases, including after a manual stop. unless-stopped strategyRespect the decision to stop manually, which does not automatically restart the container after a manual stop.
Selection recommendations:
If you want the container to run all the time, no matter what happens, choose always. If you want to be able to manually stop the container and keep it stopped, even after a Docker restart, select unless-stopped. This choice is important for managing and maintaining containers, especially when certain services need to be temporarily stopped or maintained. unless-stopped is generally considered a more flexible option, as it allows administrators to manually control the state of the container when needed.
|