人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

淺談Docker-compose中的depends_on順序的問題解決

發(fā)布日期:2022-01-29 11:18 | 文章來源:源碼中國

使用depends_on進(jìn)行容器排序時(shí)并不能完美的解決容器之間的依賴問題,原因是因?yàn)?depends_on只能保證容器進(jìn)入到 運(yùn)行狀態(tài)而不是完全狀態(tài)(不知道怎么描述了)。

網(wǎng)上已經(jīng)列出來了解決方法,使用 wait-for-it或者 wait-for,在啟動(dòng)時(shí)對需要優(yōu)先啟動(dòng)的容器進(jìn)行訪問,當(dāng)可以訪問成功時(shí)在啟動(dòng),但是都不夠詳細(xì),甚至很多都是同樣的內(nèi)容,(這里吐槽一下環(huán)境真亂)。

可能我比較笨,花了一天才解決,在這里記錄下來。

我使用的是 wait-for ,wait-for-it.sh我測試的時(shí)候了出現(xiàn)了保錯(cuò)所以沒用。

正文:

需求是要將一個(gè) 打包好的 ar文件注冊到nacos中,但是nacos啟動(dòng)較慢,就需要進(jìn)行順序的設(shè)置。

將需要的 jar文件刪除到虛擬機(jī)或服務(wù)器。接下來編寫 dockerfiledocker-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è)置落腳點(diǎn)
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é)點(diǎn)運(yùn)行
  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ī)中,因?yàn)樵?docker-compose中配置的command所使用的 文件是容器中的,如果你沒有拷貝那么將找不到文件。然后是 apt-get updateapt-get install netcat -y則是安裝 wait-for運(yùn)行的環(huán)境

我的wait-for代碼,26行為超時(shí)時(shí)間,剛開始是15,但是時(shí)間太短了 容器超時(shí)我就設(shè)置為了 60,這是我唯一跟原來不一樣的地方,之后就可以測試運(yùn)行了

#!/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)文章希望大家以后多多支持本站!

海外穩(wěn)定服務(wù)器

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部