Restart strategy of Docker container and detailed explanation of --restart option of docker run

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.

 

3. --restart option of docker run
With the --restart option, a container restart policy can be set to decide whether the Docker daemon restarts the container that just exited when the container exits.

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/

 

Copyright statement: This article is an original article by the blogger, please indicate the source when reprinting. https://blog.csdn.net/taiyangdao/article/details/73076019

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325237757&siteId=291194637