一、重启策略概述:
所谓的重启策略,指的是容器在退出运行时间,容器是否重新启动的策略
重启策略说明:
- on-failure:重启若不指定max-retries,表示重启次数不上限,等效于“always”
- on-failure:3 :表示有条件的前提下重启,当异常退出超过3次时,容器将不再重启
- unless-stopped:有异常退出,会自动重启;除非停止docker服务之前容器已被停止,否则每次启动容器时均会重启容器
- always:只要容器退出就会重启容器
- no:当容器退出时不自动重启【默认情况】
二、实战案例:
# 1、创建容器
docker container run -id --restart no --name no nginx:1.26.1-alpine
docker container run -id --restart on-failure --name on-failure nginx:1.26.1-alpine
docker container run -id --restart on-failure:3 --name on-failure-max-3 nginx:1.26.1-alpine
docker container run -id --restart unless-stopped --name unless-stopped nginx:1.26.1-alpine
docker container run -id --restart always --name always nginx:1.26.1-alpine
# 重启docker服务之前,所有的容器都处于运行状态
[root@docker101 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8056bc2f16a nginx:1.26.1-alpine "/docker-entrypoint.…" 40 seconds ago Up 39 seconds 80/tcp always
19b372d7a10d nginx:1.26.1-alpine "/docker-entrypoint.…" 41 seconds ago Up 40 seconds 80/tcp unless-stopped
54b7674a002b nginx:1.26.1-alpine "/docker-entrypoint.…" 41 seconds ago Up 40 seconds 80/tcp on-failure-max-3
86c013eef663 nginx:1.26.1-alpine "/docker-entrypoint.…" 41 seconds ago Up 40 seconds 80/tcp on-failure
d69c8b76e80b nginx:1.26.1-alpine "/docker-entrypoint.…" 41 seconds ago Up 40 seconds 80/tcp no
[root@docker101 ~]#
[root@docker101 ~]# systemctl restart docker
[root@docker101 ~]#
[root@docker101 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8056bc2f16a nginx:1.26.1-alpine "/docker-entrypoint.…" About a minute ago Up 3 seconds 80/tcp always
19b372d7a10d nginx:1.26.1-alpine "/docker-entrypoint.…" About a minute ago Up 3 seconds 80/tcp unless-stopped
54b7674a002b nginx:1.26.1-alpine "/docker-entrypoint.…" About a minute ago Up 3 seconds 80/tcp on-failure-max-3
86c013eef663 nginx:1.26.1-alpine "/docker-entrypoint.…" About a minute ago Up 3 seconds 80/tcp on-failure
d69c8b76e80b nginx:1.26.1-alpine "/docker-entrypoint.…" About a minute ago Exited (255) 4 seconds ago 80/tcp no
# 2、重启Docker服务之前,所有的容器均处于退出状态
[root@docker101 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bfeb32cf29d0 nginx:1.26.1-alpine "/docker-entrypoint.…" About a minute ago Exited (0) 1 second ago always
727353e8382a nginx:1.26.1-alpine "/docker-entrypoint.…" About a minute ago Exited (0) 1 second ago unless-stopped
9ab358a4e443 nginx:1.26.1-alpine "/docker-entrypoint.…" About a minute ago Exited (0) 1 second ago on-failure-max-3
072a9f9f033f nginx:1.26.1-alpine "/docker-entrypoint.…" About a minute ago Exited (0) 1 second ago on-failure
7518bdd85e2c nginx:1.26.1-alpine "/docker-entrypoint.…" About a minute ago Exited (0) 1 second ago no
[root@docker101 ~]#
[root@docker101 ~]# systemctl restart docker
[root@docker101 ~]#
[root@docker101 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bfeb32cf29d0 nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Up 59 seconds 80/tcp always
727353e8382a nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Exited (0) 2 minutes ago unless-stopped
9ab358a4e443 nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Exited (0) 2 minutes ago on-failure-max-3
072a9f9f033f nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Exited (0) 2 minutes ago on-failure
7518bdd85e2c nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Exited (0) 2 minutes ago no
# 3、所有容器均正常运行,使用kill -9强制杀死容器(模拟容器异常退出的情况)
[root@docker101 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bc10b4cc866 nginx:1.26.1-alpine "/docker-entrypoint.…" 7 seconds ago Up 6 seconds 80/tcp always
9369846fba4c nginx:1.26.1-alpine "/docker-entrypoint.…" 7 seconds ago Up 7 seconds 80/tcp unless-stopped
3eef20b5b943 nginx:1.26.1-alpine "/docker-entrypoint.…" 8 seconds ago Up 7 seconds 80/tcp on-failure-max-3
e0ea7088228f nginx:1.26.1-alpine "/docker-entrypoint.…" 8 seconds ago Up 7 seconds 80/tcp on-failure
ec65a251eb3c nginx:1.26.1-alpine "/docker-entrypoint.…" 8 seconds ago Up 8 seconds 80/tcp no
[root@docker101 ~]#
[root@docker101 ~]# docker container inspect -f "{{.State.Pid}}" `docker ps -aq`
5967
5886
5804
5721
5640
[root@docker101 ~]# docker container inspect -f "{{.State.Pid}}" `docker ps -aq` | xargs kill -9
[root@docker101 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bc10b4cc866 nginx:1.26.1-alpine "/docker-entrypoint.…" 2 minutes ago Up 7 seconds 80/tcp always
9369846fba4c nginx:1.26.1-alpine "/docker-entrypoint.…" 2 minutes ago Up 7 seconds 80/tcp unless-stopped
3eef20b5b943 nginx:1.26.1-alpine "/docker-entrypoint.…" 2 minutes ago Up 7 seconds 80/tcp on-failure-max-3
e0ea7088228f nginx:1.26.1-alpine "/docker-entrypoint.…" 2 minutes ago Up 8 seconds 80/tcp on-failure
ec65a251eb3c nginx:1.26.1-alpine "/docker-entrypoint.…" 2 minutes ago Exited (137) 8 seconds ago no
[root@docker101 ~]# docker container inspect -f "{{.State.Pid}}" `docker ps -aq` | xargs kill -9
Killed
[root@docker101 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bc10b4cc866 nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Up Less than a second 80/tcp always
9369846fba4c nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Up Less than a second 80/tcp unless-stopped
3eef20b5b943 nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Up Less than a second 80/tcp on-failure-max-3
e0ea7088228f nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Up Less than a second 80/tcp on-failure
ec65a251eb3c nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Exited (137) About a minute ago no
[root@docker101 ~]# docker container inspect -f "{{.State.Pid}}" `docker ps -aq` | xargs kill -9
Killed
[root@docker101 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bc10b4cc866 nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Up Less than a second 80/tcp always
9369846fba4c nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Up Less than a second 80/tcp unless-stopped
3eef20b5b943 nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Exited (137) 1 second ago on-failure-max-3
e0ea7088228f nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Up Less than a second 80/tcp on-failure
ec65a251eb3c nginx:1.26.1-alpine "/docker-entrypoint.…" 3 minutes ago Exited (137) About a minute ago no
# 查看重启策略和重启次数姿势
[root@docker101 ~]# docker container inspect -f '{{.RestartCount}}' `docker ps -aq`
4
4
3
4
0
[root@docker101 ~]# docker container inspect -f '{{.HostConfig.RestartPolicy}}' `docker ps -aq`
{always 0}
{unless-stopped 0}
{on-failure 3}
{on-failure 0}
{no 0}