服务器之家:专注于服务器技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - 服务器知识 - 浅谈Docker-compose中的depends_on顺序的问题解决

浅谈Docker-compose中的depends_on顺序的问题解决

2021-11-24 16:44莫问几 服务器知识

本文主要介绍了浅谈Docker-compose中的depends_on顺序的问题解决,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

使用depends_on进行容器排序时并不能完美的解决容器之间的依赖问题,原因是因为 depends_on只能保证容器进入到 运行状态而不是完全状态(不知道怎么描述了)。

网上已经列出来了解决方法,使用 wait-for-it或者 wait-for,在启动时对需要优先启动的容器进行访问,当可以访问成功时在启动,但是都不够详细,甚至很多都是同样的内容,(这里吐槽一下环境真乱)。

可能我比较笨,花了一天才解决,在这里记录下来。

我使用的是 wait-forwait-for-it.sh我测试的时候了出现了保错所以没用。

正文:

需求是要将一个 打包好的 ar文件注册到nacos中,但是nacos启动较慢,就需要进行顺序的设置。

浅谈Docker-compose中的depends_on顺序的问题解决

将需要的 jar文件删除到虚拟机或服务器。接下来编写 dockerfiledocker-compose

浅谈Docker-compose中的depends_on顺序的问题解决

?
1
2
3
4
5
6
7
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

浅谈Docker-compose中的depends_on顺序的问题解决

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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,这是我唯一跟原来不一样的地方,之后就可以测试运行了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/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顺序内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_49485080/article/details/121073577

延伸 · 阅读

精彩推荐