简介:docker容器化可以使得环境相对独立,减少污染。在遇到关机重启时,重新启动显得很鸡肋,特别是必须开机自启动的程序,添加必要的参数使得每次重启后可以开始使用容器非常有必要。
历史攻略:
centos7.6:安装docker
win10:安装docker和测试安装redis
Ubuntu:安装docker + 设置开机启动
设置开机启动docker:
systemctl enable docker
设置容器自动重启
1、创建容器时设置
docker run -itd --restart=always --name 容器名 使用的镜像:版本
docker 容器的重启策略:
--restart具体参数值详细信息:
no: 默认策略,容器退出时不重启容器;
on-failure: 在容器非正常退出时(退出状态非0)才重新启动容器;
on-failure:3 在容器非正常退出时重启容器,最多重启3次;
always: 无论退出状态是如何,都重启容器;
unless-stopped: 在容器退出时总是重启容器,
但是不考虑在 Docker 守护进程启动时就已经停止了的容器。
2、修改已有容器,通过update设置
如果创建时未指定 --restart=always,可通过update 命令设置
docker update --restart=always 容器ID(或者容器名)
运行结果:
(base) root@ci4vyvxi:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9dfb678e42c0 tomrunrunrun888/websiteguide:1.0.1"sh /entrypoint.sh"3 weeks ago Up 9 days 0.0.0.0:8000->80/tcp,:::8000->80/tcp websiteguide
6086859131c3 ubuntu:latest "/bin/bash"4 weeks ago Exited (137)9 days ago ssh-demo
07636c8ccd6e openspug/spug-service "/entrypoint.sh"7 weeks ago Up 9 days 0.0.0.0:8006->80/tcp,:::8006->80/tcp spug
27df7e413c07 mariadb:10.8.2"docker-entrypoint.s…"7 weeks ago Up 9 days 3306/tcp spug-db
(base) root@ci4vyvxi:~# docker update --restart=always 6086859131c3
6086859131c3
(base) root@ci4vyvxi:~# reboot ## 重启 ##
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(36.40.91.202) at 21:40:08.
Type `help' to learn how to use Xshell prompt.[C:\~]$
Connecting to xxxxxx:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-137-generic x86_64)
Last login: Sun Jan 1521:12:552023from117.140.118.233(base) root@ci4vyvxi:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9dfb678e42c0 tomrunrunrun888/websiteguide:1.0.1"sh /entrypoint.sh"3 weeks ago Up 37 seconds 0.0.0.0:8000->80/tcp,:::8000->80/tcp websiteguide
6086859131c3 ubuntu:latest "/bin/bash"4 weeks ago Up 38 seconds 0.0.0.0:1234->22/tcp,:::1234->22/tcp ssh-demo
07636c8ccd6e openspug/spug-service "/entrypoint.sh"7 weeks ago Up 37 seconds 0.0.0.0:8006->80/tcp,:::8006->80/tcp spug
27df7e413c07 mariadb:10.8.2"docker-entrypoint.s…"7 weeks ago Up 37 seconds 3306/tcp spug-db
版权归原作者 玩转测试开发 所有, 如有侵权,请联系我们删除。