Docker:docker-compose设置启动顺序

前言

项目的技术栈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

posted @ 2023-03-22 13:48  怒吼的萝卜  阅读(1445)  评论(0编辑  收藏  举报