0

我有一个包含 3 个服务的 docker-compose:config-server、cpo-events 和 cpo-executor。我希望服务仅在 config-server 准备好时启动,因为服务需要 config-server 服务中的配置文件。

码头工人-compose.yml:

version: '3.7'
services:
    config-server:
        container_name: cup-config-server
        image: cup-config-server:1.0.0
        expose:
            - 8888
        ports:
            - 8888:8888
        networks:
            - spring-cloud-network
        healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:8888/cpo-executor/dev"]
            interval: 30s
            timeout: 10s
            retries: 5
    cpo-events:
        container_name: cpo-events-kafka
        image: cpo-events-kafka:1.0.0
        expose:
            - 8878
        ports:
            - 8878:8878
        networks:
            - spring-cloud-network
        depends_on:
            - config-server
        links: 
            - config-server
    cpo-executor:
        container_name: cpo-executor
        image: cpo-executor:1.0.0
        expose:
            - 8879
        ports:
            - 8879:8879
        networks:
            - spring-cloud-network
        depends_on:
            - config-server
        links: 
            - config-server
networks:
    spring-cloud-network:
        driver: bridge

Depends_on、links、healthcheck,没有什么能保证这两个服务(cpo-executor、cpo-event)只有在 config-server 准备好后才能启动。

4

1 回答 1

1

如果您在本地测试服务,则可以添加入口点脚本

#!/usr/bin/env bash

wait_single_host() {
  local host=$1
  shift
  local port=$1
  shift

  echo "==> Check host ${host}:${port}"
  while ! nc ${host} ${port} > /dev/null 2>&1 < /dev/null; do echo "   --> Waiting for ${host}:${port}" && sleep 1; done;
}

wait_all_hosts() {
  if [ ! -z "$WAIT_FOR_HOSTS" ]; then
    local separator=':'
    for _HOST in $WAIT_FOR_HOSTS ; do
        IFS="${separator}" read -ra _HOST_PARTS <<< "$_HOST"
        wait_single_host "${_HOST_PARTS[0]}" "${_HOST_PARTS[1]}"
    done
  else
    echo "IMPORTANT : Waiting for nothing because no $WAIT_FOR_HOSTS env var defined !!!"
  fi
}

wait_all_hosts
sleep 60

bash -c "/usr/local/bin/docker-entrypoint $*"

您可以在此处查看完整的示例用法。它解决了相同的问题,但针对 docker-compose 3.7 版的不同用例

于 2021-02-10T14:05:22.323 回答