在docker-compose中处理链接容器的依赖性

In docker-compose a common problem is starting services and daemons in containers that depends on services running on linked containers, in example: your app depends on elasticsearch but it is not ready when the app container is started. Solution is to use a wait script.

docker-compose中,一个常见的问题是在依赖于在链接容器上运行的服务的容器中启动服务和守护程序,例如:您的应用程序依赖于Elasticsearch,但在启动应用程序容器时尚未准备就绪。 解决方案是使用等待脚本。

I knew that docker-compose team is working on adding a WAIT parameter, but while it is not ready we can use a wait script to load our services.

我知道docker-compose团队正在努力添加WAIT参数,但是当它还没有准备好时,我们可以使用等待脚本来加载我们的服务。

The process is simple, your container will execute a shell script that will try to execute a HEALTH CHECK many times you want before starting the main command. The health check, the command, the sleep and how many loops to try will be defined in environment variables

该过程很简单,您的容器将执行一个shell脚本,该脚本将在启动main命令之前尝试多次执行一次HEALTH CHECK。 运行状况检查,命令,睡眠和尝试尝试的循环次数将在环境变量中定义

This is a program that needs to access an elasticsearch server located in ‘elastic’ host (that will be mapped by compose

这是一个程序,需要访问位于“弹性”主机中的elasticsearch服务器(该文件将通过组合

access_elastic_search.py

access_elastic_search.py

from datetime import datetime 
from elasticsearch import Elasticsearch
es = Elasticsearch('elastic')
es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()})
print es.get(index="my-index", doc_type="test-type", id=42)
from datetime import datetime 
from elasticsearch import Elasticsearch
es = Elasticsearch('elastic')
es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()})
print es.get(index="my-index", doc_type="test-type", id=42)
 

docker-compose.yml

docker-compose.yml

wait_to_start script

wait_to_start脚本

 #!/bin/bash

echo $WAIT_COMMAND
echo $WAIT_START_CMD

is_ready() {
    eval "$WAIT_COMMAND"
}

# wait until is ready
i=0
while ! is_ready; do
    i=`expr $i + 1`
    if [ $i -ge $WAIT_LOOPS ]; then
        echo "$(date) - still not ready, giving up"
        exit 1
    fi
    echo "$(date) - waiting to be ready"
    sleep $WAIT_SLEEP
done

#start the script
exec $WAIT_START_CMD
 #!/bin/bash

echo $WAIT_COMMAND
echo $WAIT_START_CMD

is_ready() {
    eval "$WAIT_COMMAND"
}

# wait until is ready
i=0
while ! is_ready; do
    i=`expr $i + 1`
    if [ $i -ge $WAIT_LOOPS ]; then
        echo "$(date) - still not ready, giving up"
        exit 1
    fi
    echo "$(date) - waiting to be ready"
    sleep $WAIT_SLEEP
done

#start the script
exec $WAIT_START_CMD
 

Now when you start your environment with docker-compose up the app container will start and execute wait_to_start script, which will perform the test defined in WAIT_COMMAND environment variable and will retry until the test succeeds, so it will end executing the program defined in WAIT_START_CMD

现在,当您使用docker-compose up启动环境docker-compose up ,应用程序容器将启动并执行wait_to_start脚本,该脚本将执行WAIT_COMMAND环境变量中定义的测试,并将重试直到测试成功,因此它将结束执行WAIT_START_CMD定义的程序

翻译自: https://www.pybloggers.com/2015/07/dealing-with-linked-containers-dependency-in-docker-compose/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值