咱们的 Centos 服务器上部署了好多个 docker 容器,因故重启的时候就会致使还得手动去手动重启这些 docker 容器,为何不写个脚本自动重启呢,因而就有了这篇文章。html
以前咱们有提到过关于 docker 的一些骚操做 ,能够直接使用docker
docker start $(docker ps -aq) # 启动全部容器 docker start $(docker ps -aq -f status=exited) # 启动全部状态为exited 的容器,和上面的命令效果一致
对于有 link 有依赖项的须要先把对应的依赖项先启动,好比说咱们有一个 identityserver 的 docker 容器(auth-server),别的容器启动的时候会 link 它,这时候就须要先启动 auth-server 再启动其余容器,这时启动脚本则须要稍加修改shell
docker start auth-server # 先启动 auth-server 容器 docker start $(docker ps -aq -f status=exited) # 再启动全部状态为exited 的容器
写一个启动 startup 脚本,在系统启动的时候执行它
在一个你想放启动脚本的地方建立一个 startup.sh
,我这里建立在了 /usr/local/scripts/startup.sh
bash
文件内容以下:服务器
#!/bin/bash # start docker container docker start auth-server docker start $(docker ps -aq -f status=exited)
设置文件权限:ide
sudo chmod +x /usr/local/scripts/startup.sh
在 /etc/rc.d/rc.local
文件中添加开机启动执行脚本this
sudo vi /etc/rc.d/rc.local
编辑文件,添加自定义的启动脚本spa
#!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local /usr/local/scripts/startup.sh # 新增自定义启动脚本
设置文件权限code
chmod +x /etc/rc.d/rc.local
执行 sudo reboot
重启服务器,稍后从新链接,执行 docker ps
查看在运行的 docker 镜像,有 docker 在运行就说明咱们的启动脚本正常执行了~~server