Docker容器依賴link連接按順序啟動(dòng)方式
Docker 中的容器一般都會(huì)遇到相互之間服務(wù)依賴的問題,通過 --link 指定一個(gè)或多個(gè)依賴容器服務(wù),但啟動(dòng)的時(shí)候卻因?yàn)槿萜鲀?nèi)的服務(wù)啟動(dòng)的延時(shí)不確定,并不能保證所有容器都能成功啟動(dòng),于是需要強(qiáng)制按容器內(nèi)的服務(wù)順序來啟動(dòng)容器。
需要一個(gè)工具
先下載這個(gè)腳本, 然后根據(jù)項(xiàng)目中容器的服務(wù)依賴順序配置就可以了, 唯一的問題就是服務(wù)都需要有可訪問的端口.
舉個(gè)栗子
nginx 依賴 php-fpm 依賴 redis | mysql
那么就倒序啟動(dòng), web_up.sh
wait_for_it=/zzstore/my-tools/wait-for-it/wait-for-it.sh docker container start my-mysql-8.0 && \ $wait_for_it -s -t 15 localhost:16033 -- echo 'mysql is up ~' && docker container start redis-6.2-19736 && \ $wait_for_it -s -t 15 localhost:19736 -- echo 'redis-19736 is up ~' && docker container start redis-6.2-19740 && \ $wait_for_it -s -t 15 localhost:19740 -- echo 'redis-19740 is up ~' && docker container start my-php-8.0-fpm && \ $wait_for_it -s -t 15 localhost:9080 -- echo 'php-fpm is up ~' && docker container start my-nginx-1.20 && \ $wait_for_it -s -t 15 localhost:80 -- echo 'nginx is up ~'
-s: 表示嚴(yán)格模式, 若檢測(cè)的服務(wù)在-t: xx秒 內(nèi)沒有啟動(dòng), 則終止后續(xù)所有命令.
這個(gè)腳本是在宿主機(jī)上運(yùn)行的, 監(jiān)聽的端口也都是宿主機(jī)上的端口, 如: 16033 映射 mysql 容器內(nèi) 3306
也可以用來監(jiān)聽 Docker 服務(wù)的啟動(dòng)狀態(tài):
wait_for_it=/zzstore/my-tools/wait-for-it/wait-for-it.sh $wait_for_it -s -t 60 localhost:2375 -- echo 'docker is up ~' && docker container start my-mysql-8.0 && \ $wait_for_it -s -t 15 localhost:16033 -- echo 'mysql is up ~' && docker container start redis-6.2-19736 && \ $wait_for_it -s -t 15 localhost:19736 -- echo 'redis-19736 is up ~' && docker container start redis-6.2-19740 && \ $wait_for_it -s -t 15 localhost:19740 -- echo 'redis-19740 is up ~' && docker container start my-php-8.0-fpm && \ $wait_for_it -s -t 15 localhost:9080 -- echo 'php-fpm is up ~' && docker container start my-nginx-1.20 && \ $wait_for_it -s -t 15 localhost:80 -- echo 'nginx is up ~'
等 Dockerd 服務(wù)啟動(dòng)后自動(dòng)按服務(wù)順序啟動(dòng)所有容器.
至于 -t 參數(shù)的時(shí)長(zhǎng)可以在服務(wù)器上先測(cè)試一下根據(jù)實(shí)際情況再調(diào)整下.
全部正常成功啟動(dòng)的效果圖

上面的啟動(dòng)命令每個(gè) -- 后的所有命令都是一個(gè)整體, 前面沒有執(zhí)行成功后面都不會(huì)觸發(fā).
再來測(cè)試下啟動(dòng)失敗的情況
當(dāng)前容器全部已啟動(dòng)的狀態(tài), 我們關(guān)閉其中一個(gè)依賴的服務(wù) redis-6.2-19740 然后再執(zhí)行腳本.
docker container stop redis-6.2-19740
wait_for_it=/zzstore/my-tools/wait-for-it/wait-for-it.sh $wait_for_it -s -t 60 localhost:2375 -- echo 'docker is up ~' && docker container start my-mysql-8.0 && \ $wait_for_it -s -t 15 localhost:16033 -- echo 'mysql is up ~' && docker container start redis-6.2-19736 && \ #$wait_for_it -s -t 15 localhost:19736 -- echo 'redis-19736 is up ~' && docker container start redis-6.2-19740 && \ $wait_for_it -s -t 15 localhost:19740 -- echo 'redis-19740 is up ~' && docker container start my-php-8.0-fpm && \ $wait_for_it -s -t 15 localhost:9080 -- echo 'php-fpm is up ~' && docker container start my-nginx-1.20 && \ $wait_for_it -s -t 15 localhost:80 -- echo 'nginx is up ~'

wait-for-it.sh: timeout occurred after waiting 15 seconds for localhost:19740 wait-for-it.sh: strict mode, refusing to execute subprocess
由于 redis-6.2-19740 沒有啟動(dòng), 而且使用了 -s 的 strict mode 因此后續(xù)存在依賴的命令全都不會(huì)執(zhí)行了.
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
聊聊kubernetes1.20用containerd替換docker(shim)的問題
這篇文章主要介紹了kubernetes1.20用containerd替換docker(shim),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
Docker安裝mysql主從復(fù)制的實(shí)現(xiàn)步驟
MySQL主從復(fù)制是指數(shù)據(jù)可以從一個(gè)MySQL數(shù)據(jù)庫(kù)服務(wù)器主節(jié)點(diǎn)復(fù)制到一個(gè)或多個(gè)從節(jié)點(diǎn),本文主要介紹了Docker安裝mysql主從復(fù)制的實(shí)現(xiàn)步驟,感興趣的可以了解一下2024-03-03
docker nginx + https 子域名配置詳細(xì)教程
這篇文章主要介紹了docker nginx + https 子域名配置詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Docker中"exec format error"錯(cuò)誤架構(gòu)不匹配的原因分析和解
本文詳細(xì)介紹了Docker中“execformaterror”錯(cuò)誤的成因,并提供了兩種解決方案:使用匹配架構(gòu)的鏡像和啟用QEMU模擬器,推薦用戶使用匹配架構(gòu)的鏡像以解決架構(gòu)不匹配的問題,感興趣的朋友跟隨小編一起看看吧2026-01-01

