如何解决Docker-compose中depends_on顺序的问题
发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,小编给大家分享一下如何解决Docker-compose中depends_on顺序的问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了
千家信息网最后更新 2026年11月04日如何解决Docker-compose中depends_on顺序的问题
小编给大家分享一下如何解决Docker-compose中depends_on顺序的问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
正文:
需求是要将一个 打包好的 ar文件注册到nacos中,但是nacos启动较慢,就需要进行顺序的设置。
将需要的 jar文件删除到虚拟机或服务器。接下来编写 dockerfile和docker-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 update和 apt-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=60QUIET=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 >&2Usage: $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 finishesUSAGE 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 ;; esacdoneif ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then echoerr "Error: invalid timeout '$TIMEOUT'" usage 3ficase "$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 ;;esacwait_for "$@"以上是"如何解决Docker-compose中depends_on顺序的问题"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
文件
顺序
拷贝
篇文章
运行
问题
代码
内容
容器
时间
配置
平常
重要
接下来
不大
不怎么
图片
地方
大部分
就是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技图片
广州大一互联网络科技有限公司
互联网电商科技
互联网科技公司税收优惠
互联网科技有限公司图片
互联网科技视频
上海致骋互联网科技
融贸通互联网科技
狮子座互联网科技培训
科技互联网创业计划书
互联网科技宣传片
互联网养老 桔豆科技
四个字互联网科技公司取名
欧洲十大科技互联网公司
崇明区互联网教育科技诚信经营
互联网科技大赛获奖感言
济南有互联网科技公司吗
长沙互联网科技
上海乐五互联网科技有限公司
互联网科技新闻
与互联网科技公司
浩特互联网科技
互联网科技logo
顶级互联网科技成果有哪些
互联网软件和科技
亳州好赞互联网科技有限公司
特约风豹互联网科技为什么扣费
互联网科技手机排行榜
山东蛋芒互联网科技有限公司
青岛互联网科技园