A brief discussion on how to solve the depends_on order problem in Docker-compose

A brief discussion on how to solve the depends_on order problem in Docker-compose

Using depends_on to sort containers does not perfectly solve the dependency problem between containers. The reason is that depends_on can only ensure that the container enters the running state rather than the complete state (I don’t know how to describe it).

Solutions have been listed online, using wait-for-it or wait-for to access the containers that need to be started first at startup, and start them when access is successful. However, they are not detailed enough, and many of them even have the same content (here I would like to complain about the messy environment).

Maybe I'm stupid, it took me a day to solve it, so I record it here.

I used wait-for , wait-for-it.sh gave an error when I tested it, so it was useless.

text:

The requirement is to register a packaged ar file into nacos, but nacos starts slowly, so sequential settings are required.

]

Delete the required jar files to the virtual machine or server. Next, write dockerfile and docker-compose

insert image description here

FROM openjdk:8-jre // Based on openjdk 
COPY wait-for . //Copy wait-for to the virtual machine RUN apt-get update //Update source,
RUN apt-get install netcat -y // Install netcat, wait-for requires the use of WORKDIR /app // Set the landing point ADD course.jar course.jar // Add the jar file to the virtual machine EXPOSE 8002 // The port to be exposed

My configuration is based on the picture, and the code block is for better understanding. I think there should be no difference.

docker-compose

insert image description here

version: '3.0'
services:
  nacos:
    image: nacos/nacos-server:1.1.4
    container_name: nacos
    ports:
      - "8848:8848"
    environment:
      MODE: standalone # nacos single node running course:
    build: /root/
    container_name: course
    ports:
      - "18002:18002"
    depends_on:
      -nacos
    command: ["sh","wait-for","nacos:8848","--","java","-jar","course.jar"]

I won't go into too much explanation here, it's not much different from usual.

The post I found before did not post the dockerfile file. The most important thing here is to copy wait-for file to the virtual machine, because the file used by the command configured in docker-compose is in the container. If you don’t copy it, you will not find the file. Then apt-get update and apt-get install netcat -y are used to install wait-for running environment

In my wait-for code, line 26 is the timeout period. It was 15 at first, but the time was too short and the container timeout was set to 60. This is the only difference from the original. You can test and run it later.

#!/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 "$@"

This is the end of this article about the solution to the depends_on order problem in Docker-compose. For more relevant Docker-compose depends_on order content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Practical notes on installing Jenkins with docker-compose
  • Detailed process of getting started with docker compose helloworld
  • Docker Compose installation and usage steps
  • Detailed explanation of the Sidecar mode in Docker Compose

<<:  CSS 3.0 text hover jump special effects code

>>:  Practical operation of using any font in a web page with demonstration

Recommend

Ansible automated operation and maintenance deployment method for Linux system

Ansible is a new automated operation and maintena...

JS realizes the calculation of the total price of goods in the shopping cart

JS calculates the total price of goods in the sho...

Detailed explanation of Vue project optimization and packaging

Table of contents Preface 1. Routing lazy loading...

Implementation of LNMP for separate deployment of Docker containers

1. Environmental Preparation The IP address of ea...

How to obtain a permanent free SSL certificate from Let's Encrypt in Docker

1. Cause The official cerbot is too annoying. It ...

Detailed explanation of how to create MySql scheduled tasks in navicat

Detailed explanation of creating MySql scheduled ...

How to mount a disk in Linux

When using a virtual machine, you may find that t...

Implementation of fuzzy query like%% in MySQL

1, %: represents any 0 or more characters. It can...

The connection between JavaScript and TypeScript

Table of contents 1. What is JavaScript? 2. What ...

Change the MySQL database engine to InnoDB

PS: I use PHPStudy2016 here 1. Stop MySQL during ...

mysql query data for today, this week, this month, and last month

today select * from table name where to_days(time...

How to insert a link in html

Each web page has an address, identified by a URL...

Detailed explanation of MySQL Strict Mode knowledge points

I. Strict Mode Explanation According to the restr...

JS+CSS to realize dynamic clock

This article example shares the specific code of ...