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

使用depends_on進行容器排序時並不能完美的解決容器之間的依賴問題,原因是因為 depends_on隻能保證容器進入到 運行狀態而不是完全狀態(不知道怎麼描述瞭)。

網上已經列出來瞭解決方法,使用 wait-for-it或者 wait-for,在啟動時對需要優先啟動的容器進行訪問,當可以訪問成功時在啟動,但是都不夠詳細,甚至很多都是同樣的內容,(這裡吐槽一下環境真亂)。

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

我使用的是 wait-forwait-for-it.sh我測試的時候瞭出現瞭保錯所以沒用。

正文:

需求是要將一個 打包好的 ar文件註冊到nacos中,但是nacos啟動較慢,就需要進行順序的設置。

]

將需要的 jar文件刪除到虛擬機或服務器。接下來編寫 dockerfiledocker-compose

在這裡插入圖片描述

FROM openjdk:8-jre	// 基於 openjdk 
COPY wait-for .		// 將 wait-for 拷貝到虛擬機中
RUN apt-get update	// 更新源,
RUN apt-get install netcat -y // 安裝 netcat, wait-for 需要使用到
WORKDIR /app				// 設置落腳點
ADD course.jar course.jar	// 將 jar 文件 添加到 虛擬機中
EXPOSE 8002					// 需要暴露的端口

我的配置是以圖片為準的,代碼塊中的是為瞭更好的理解 ,想來應該沒有差別。

docker-compose

在這裡插入圖片描述

version: '3.0'
services:
  nacos:
    image: nacos/nacos-server:1.1.4
    container_name: nacos
    ports:
      - "8848:8848"
    environment:
      MODE: standalone   # nacos 單節點運行

  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文件拷貝到虛擬機中,因為在 docker-compose中配置的command所使用的 文件是容器中的,如果你沒有拷貝那麼將找不到文件。然後是 apt-get updateapt-get install netcat -y則是安裝 wait-for運行的環境

我的wait-for代碼,26行為超時時間,剛開始是15,但是時間太短瞭 容器超時我就設置為瞭 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 "$@"

到此這篇關於淺談Docker-compose中的depends_on順序的問題解決的文章就介紹到這瞭,更多相關Docker-compose depends_on順序內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: