https://blog.csdn.net/taiyangdao/article/details/73076019
1. Docker container restart strategy
The restart strategy of the Docker container is a startup strategy for the production environment, which can be ignored during the development process.
The restart of Docker containers is all done by the Docker daemon, so it is closely related to the daemon.
The restart strategy for Docker containers is as follows:
- no, the default policy, do not restart the container when the container exits
- on-failure, when the container exits abnormally (exit status is not 0), the container will be restarted
- on-failure: 3, restart the container when the container exits abnormally, restart up to 3 times
- always, always restart the container when the container exits
- unless-stopped, always restart the container when it exits, but ignore containers that were stopped when the Docker daemon started
2. Exit status code of Docker container
The exit status code of docker run is as follows:
- 0, means exit normally
- Non-0, indicating an abnormal exit (the exit status code adopts the chroot standard)
- 125, Error in the Docker daemon itself
- 126. After the container is started, the default command to be executed cannot be called
- 127. After the container starts, the default command to be executed does not exist
- Other command status codes. After the container starts, the command is executed normally. When the command is exited, the return status code of the command is used as the exit status code of the container.
The --restart option is usually only used for containers in detached mode.
The --restart option cannot be used with the --rm option. Apparently, the --restart option applies to containers in detached mode, while the --rm option applies to containers in foreground mode.
When viewing a container with docker ps, for a container with the --restart option, the only possible states are Up or Restarting.
示例:
docker run -d --restart=always bba-208
docker run -d --restart=on-failure:10 bba-208
Replenish:
Check the number of container restarts
docker inspect -f "{{ .RestartCount }}" bba-208
Check the last start time of the container
docker inspect -f "{{ .State.StartedAt }}" bba-208
Reference link:
https://docs.docker.com/engine/reference/run/