首页 > 其他分享 >Docker:docker-compose设置启动顺序

Docker:docker-compose设置启动顺序

时间:2023-03-22 13:55:05浏览次数:49  
标签:compose -- redis rabbitmq ## TIMEOUT Docker docker

前言

项目的技术栈vue+nodejs+redis+rabbitmq,由于vue服务依赖于redis+rabbitmq组件,所以需要先启动redis+rabbitmq组件,之后在运行vue服务。

属性设置

version: "3"
services:
    ## redis
    redis:
        image: redis:latest
        ports:
            - "6379:6379"
        container_name: im-redis-compose
        restart: always
        command: redis-server --appendonly yes
    
    ## rabbitmq
    rabbitmq:
        image: rabbitmq:management
        ports:
            - "5672:5672"
            - "15672:15672"
        container_name: im-rabbitmq-compose
        environment:
            RABBITMQ_DEFAULT_USER: guest
            RABBITMQ_DEFAULT_PASS: guest
            RABBITMQ_DEFAULT_VHOST: my_vhost

    ## vue服务
    backend:
        build: .
        links:
            - redis
            - rabbitmq
        container_name: im-server-compose
        restart: on-failure
        ## 设置启动顺序
        depends_on:
            - rabbitmq
            - redis
        ports:
            - "3000:3000"

问题

运行 docker-compose up 命令的时候却报错,发现是vue服务的nodejs报的错,启动的时候报错connect refused,连接rabbitmq的时候出错;明明设置depends_on属性启动顺序,结果没有生效。

原因

depends_on 在启动 vue 服务容器前,并不会等待 rabbitmq 和 redis 这两个容器进入ready状态,而只是等到它们被启动状态(被启动,但不关心是否启动完成);所以导致 rabbitmq 和 redis 这个两个容器虽然先启动,但是在 vue 服务容器之后才启动完成。

具体内容可以自己去看https://docs.docker.com/compose/startup-order/

解决方法

在容器启动命令执行前,跑一个shell脚本,这个脚本会去访问依赖服务的页面或者ping api来判断底层的服务有没有ready,随后再去启动真正的服务。

GitHub有现成的方案:wait-for

1. 修改vue服务的DockerFile文件(需要联网)

添加 RUN apt-get -q update && apt-get -qy install netcat 命令

#基础镜像
FROM nginx:stable

##定义工作目录
ENV WORK_PATH /etc/nginx
#
##定义conf文件名
ENV CONF_FILE_NAME nginx.conf
#
##更新apt-get 下载netcat服务
RUN apt-get -q update && apt-get -qy install netcat
#
##删除原有配置文件
RUN rm $WORK_PATH/$CONF_FILE_NAME
#
##复制新的配置文件
COPY ./$CONF_FILE_NAME $WORK_PATH/
#
##给shell文件赋读权限
RUN chmod a+r $WORK_PATH/$CONF_FILE_NAME

 2. 将wait-for.sh复制到当前文件夹

 wait-for.sh

查看代码
 #!/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.

VERSION="2.2.4"

set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result"
TIMEOUT=15
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
                                      Defaults to 15 seconds
  -v | --version                      Show the version of this tool
  -- 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
      ;;
    http)
      if ! command -v wget >/dev/null; then
        echoerr 'wget command is missing!'
        exit 1
      fi
      ;;
  esac

  TIMEOUT_END=$(($(date +%s) + TIMEOUT))

  while :; do
    case "$PROTOCOL" in
      tcp)
        nc -w 1 -z "$HOST" "$PORT" > /dev/null 2>&1
        ;;
      http)
        wget --timeout=1 --tries=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 -ne 0 -a $(date +%s) -ge $TIMEOUT_END ]; then
      echo "Operation timed out" >&2
      exit 1
    fi

    sleep 1
  done
}

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
    ;;
    -v | --version)
    echo $VERSION
    exit
    ;;
    -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 "$@"
Footer

 3. 修改docker-compose.yml文件

version: "3"
services:
    redis:
        image: redis:latest
        ports:
            - "6379:6379"
        container_name: im-redis-compose
        restart: always
        command: redis-server --appendonly yes
    
    rabbitmq:
        image: rabbitmq:management
        ports:
            - "5672:5672"
            - "15672:15672"
        container_name: im-rabbitmq-compose
        environment:
            RABBITMQ_DEFAULT_USER: guest
            RABBITMQ_DEFAULT_PASS: guest
            RABBITMQ_DEFAULT_VHOST: my_vhost

    backend:
        build: .
        links:
            - redis
            - rabbitmq
        container_name: im-server-compose
        restart: on-failure
        depends_on:
            - rabbitmq
            - redis
        ports:
            - "3000:3000"
        ## 添加运行脚本的命令
        command: sh -c './wait-for.sh rabbitmq:15672 -- npm run start'

 

文章转载至:https://segmentfault.com/a/1190000021504344

标签:compose,--,redis,rabbitmq,##,TIMEOUT,Docker,docker
From: https://www.cnblogs.com/nhdlb/p/17243468.html

相关文章

  • 源码共读 | koa-compose
    前言学习目标:koa-compose洋葱模型资源:源码地址:koajs/composekoa-composeKoa-compose是一个Koa中间件工具,Koa是一个流行的Node.js网络框架。Koa-compose允......
  • .netcore 在Linux(Centos)使用Docker方式部署
    运行环境假设你已经安装好了.netcore运行环境,未配置可以看这篇​​[linux(centos)搭建.netcore运行环境]​​centos:7.2cpu:1核2G内存1M带宽docker:18.06.1-ce安装docke......
  • Docker - nextcloud
    开发同学直接打包发到群里,包名都一样,没有版本的概念;包环境也无法区分,所以想自己做个部门的归档服务。$dockerpullnextcloud$dockerrun-d--restart=always--nam......
  • docker-compose 部署文件预览服务-kkFileView
    docker-compose部署文件预览服务1.docker-compose文件准备docker-compose-fileview.ymlversion:'3.2'services:fileview:image:keking/kkfileview:lates......
  • 配置docker容器veth-pair---实现桥接模式
    前言:已知docker网络三种基础模式bridge、host、none,·bridge:桥接模式,创建容器时默认的网络模式;docker安装时,在宿主机内创建一个虚拟网桥docker0,并自动给docker......
  • 【docker】解决报错 WARNING: IPv4 forwarding is disabled. Networking will not wor
    https://blog.csdn.net/weixin_68100450/article/details/126054580一,docker运行直接报错报错:[root@localhost/]#dockerrun-itubuntu/bin/bashWARNING:IPv4f......
  • Docker CLI docker config ls 常用命令
    Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的Linux或Windows操作系统的机器上,也可以实现虚拟化。Doc......
  • rap2单独docker容器部署
    rap2docker容器部署,前后端docker部署官方提供了docker-compose安装步骤,本以为一键搞定,结果服务器上docker-compse与docker版本冲突,搞不定啊搞不定,无奈只能手动建容器部署......
  • Docker镜像的制作和管理
    一、镜像的制作方式​Docker镜像制作类似于虚拟机的镜像(模版)制作,即按照公司的实际业务需求将需要安装的软件、相关配置等基础环境配置完成,然后将其做成镜像,最后再批量从镜......
  • docker安装nginx
    开放端口8880查询centos7的8880端口firewall-cmd--query-port=8880/tcpno开放端口firewall-cmd--add-port=8880/tcp--permanentsuccess重新载入防火墙fi......