淺談Docker-compose中的depends_on順序的問題解決
使用depends_on進(jìn)行容器排序時并不能完美的解決容器之間的依賴問題,原因是因為 depends_on只能保證容器進(jìn)入到 運行狀態(tài)而不是完全狀態(tài)(不知道怎么描述了)。
網(wǎng)上已經(jīng)列出來了解決方法,使用 wait-for-it或者 wait-for,在啟動時對需要優(yōu)先啟動的容器進(jìn)行訪問,當(dāng)可以訪問成功時在啟動,但是都不夠詳細(xì),甚至很多都是同樣的內(nèi)容,(這里吐槽一下環(huán)境真亂)。
可能我比較笨,花了一天才解決,在這里記錄下來。
我使用的是 wait-for ,wait-for-it.sh我測試的時候了出現(xiàn)了保錯所以沒用。
正文:
需求是要將一個 打包好的 ar文件注冊到nacos中,但是nacos啟動較慢,就需要進(jìn)行順序的設(shè)置。

將需要的 jar文件刪除到虛擬機(jī)或服務(wù)器。接下來編寫 dockerfile和docker-compose

FROM openjdk:8-jre // 基于 openjdk COPY wait-for . // 將 wait-for 拷貝到虛擬機(jī)中 RUN apt-get update // 更新源, RUN apt-get install netcat -y // 安裝 netcat, wait-for 需要使用到 WORKDIR /app // 設(shè)置落腳點 ADD course.jar course.jar // 將 jar 文件 添加到 虛擬機(jī)中 EXPOSE 8002 // 需要暴露的端口
我的配置是以圖片為準(zhǔn)的,代碼塊中的是為了更好的理解 ,想來應(yīng)該沒有差別。
docker-compose

version: '3.0'
services:
nacos:
image: nacos/nacos-server:1.1.4
container_name: nacos
ports:
- "8848:8848"
environment:
MODE: standalone # nacos 單節(jié)點運行
course:
build: /root/
container_name: course
ports:
- "18002:18002"
depends_on:
- nacos
command: ["sh","wait-for","nacos:8848","--","java","-jar","course.jar"]
這里就不做過多的解釋了,與平常相差不大。
我之前查找到的帖子中,沒有貼出dockerfile文件在這里最重要的就是,將 wait-for文件拷貝到虛擬機(jī)中,因為在 docker-compose中配置的command所使用的 文件是容器中的,如果你沒有拷貝那么將找不到文件。然后是 apt-get update和 apt-get install netcat -y則是安裝 wait-for運行的環(huán)境
我的wait-for代碼,26行為超時時間,剛開始是15,但是時間太短了 容器超時我就設(shè)置為了 60,這是我唯一跟原來不一樣的地方,之后就可以測試運行了
#!/bin/sh
# The MIT License (MIT)
#
# Copyright (c) 2017 Eficode Oy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result"
TIMEOUT=60
QUIET=0
# The protocol to make the request with, either "tcp" or "http"
PROTOCOL="tcp"
echoerr() {
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
}
usage() {
exitcode="$1"
cat << USAGE >&2
Usage:
$0 host:port|url [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit "$exitcode"
}
wait_for() {
case "$PROTOCOL" in
tcp)
if ! command -v nc >/dev/null; then
echoerr 'nc command is missing!'
exit 1
fi
;;
wget)
if ! command -v wget >/dev/null; then
echoerr 'wget command is missing!'
exit 1
fi
;;
esac
while :; do
case "$PROTOCOL" in
tcp)
nc -w 1 -z "$HOST" "$PORT" > /dev/null 2>&1
;;
http)
wget --timeout=1 -q "$HOST" -O /dev/null > /dev/null 2>&1
;;
*)
echoerr "Unknown protocol '$PROTOCOL'"
exit 1
;;
esac
result=$?
if [ $result -eq 0 ] ; then
if [ $# -gt 7 ] ; then
for result in $(seq $(($# - 7))); do
result=$1
shift
set -- "$@" "$result"
done
TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7
shift 7
exec "$@"
fi
exit 0
fi
if [ "$TIMEOUT" -le 0 ]; then
break
fi
TIMEOUT=$((TIMEOUT - 1))
sleep 1
done
echo "Operation timed out" >&2
exit 1
}
while :; do
case "$1" in
http://*|https://*)
HOST="$1"
PROTOCOL="http"
shift 1
;;
*:* )
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
shift 1
;;
-q | --quiet)
QUIET=1
shift 1
;;
-q-*)
QUIET=0
echoerr "Unknown option: $1"
usage 1
;;
-q*)
QUIET=1
result=$1
shift 1
set -- -"${result#-q}" "$@"
;;
-t | --timeout)
TIMEOUT="$2"
shift 2
;;
-t*)
TIMEOUT="${1#-t}"
shift 1
;;
--timeout=*)
TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
break
;;
--help)
usage 0
;;
-*)
QUIET=0
echoerr "Unknown option: $1"
usage 1
;;
*)
QUIET=0
echoerr "Unknown argument: $1"
usage 1
;;
esac
done
if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then
echoerr "Error: invalid timeout '$TIMEOUT'"
usage 3
fi
case "$PROTOCOL" in
tcp)
if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then
echoerr "Error: you need to provide a host and port to test."
usage 2
fi
;;
http)
if [ "$HOST" = "" ]; then
echoerr "Error: you need to provide a host to test."
usage 2
fi
;;
esac
wait_for "$@"
到此這篇關(guān)于淺談Docker-compose中的depends_on順序的問題解決的文章就介紹到這了,更多相關(guān)Docker-compose depends_on順序內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信