咱們的 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
centos
文件內容以下:bash
#!/bin/bash # start docker container docker start auth-server docker start $(docker ps -aq -f status=exited)
設置文件權限:服務器
sudo chmod +x /usr/local/scripts/startup.sh
在 /etc/rc.d/rc.local
文件中添加開機啓動執行腳本ide
sudo vi /etc/rc.d/rc.local
編輯文件,添加自定義的啓動腳本this
#!/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