我正在尝试编写一个Bash脚本,该脚本将停止Docker容器的存储库,重新构建它们,并对它们运行一些测试(使用Pytest)。为了使代码干燥,我尝试定义一个函数wait_for_container
,如下所示:
docker stop $(docker ps -a -q)
docker-compose build
docker-compose up -d
function wait_for_container {
local CONTAINER=$1
local PORT=$2
ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER)
until nc -z $CONTAINER $PORT
do
echo "Waiting for the $CONTAINER container..."
sleep 0.5
done
echo "$CONTAINER listening at $ADDR:$PORT"
}
RETHINKDB_CONTAINER=ipercroncompose_rethinkdb_1
RETHINKDB_PORT=28015
wait_for_container $RETHINKDB_CONTAINER $RETHINKDB_PORT
RABBITMQ_CONTAINER=ipercroncompose_rabbitmq_1
RABBITMQ_PORT=5672
wait_for_container $RABBITMQ_CONTAINER $RABBITMQ_PORT
cd test
pytest
但是,我发现这行不通:我反复得到
nc: getaddrinfo: Temporary failure in name resolution
Waiting for the ipercroncompose_rethinkdb_1 container...
另一方面,以下非DRY脚本可以正常工作:
docker stop $(docker ps -a -q)
docker-compose build
docker-compose up -d
RETHINKDB_CONTAINER=ipercroncompose_rethinkdb_1
RETHINKDB_ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $RETHINKDB_CONTAINER)
RETHINKDB_PORT=28015
until nc -z $RETHINKDB_ADDR $RETHINKDB_PORT
do
echo "Waiting for the RethinkDB container..."
sleep 0.5
done
echo "RethinkDB listening at ${RETHINKDB_ADDR}:${RETHINKDB_PORT}."
RABBITMQ_CONTAINER=ipercroncompose_rabbitmq_1
RABBITMQ_ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $RABBITMQ_CONTAINER)
RABBITMQ_PORT=5672
until nc -z $RABBITMQ_ADDR $RABBITMQ_PORT
do
echo "Waiting for the RabbitMQ container..."
sleep 0.5
done
echo "RabbitMQ listening at ${RABBITMQ_ADDR}:${RABBITMQ_PORT}."
cd test
pytest
和回声
RethinkDB listening at 172.18.0.2:28015.
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
RabbitMQ listening at 172.19.0.2:5672.
其次是Pytests的结果。如何改善wait_for_container
功能以达到相同的效果?
在Grisha Levit和Fred的评论之后,以下是改编的脚本:
docker stop $(docker ps -a -q)
docker-compose build
docker-compose up -d
function wait_for_container {
local CONTAINER=$1
local PORT=$2
local ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER)
echo $ADDR
until nc -z $ADDR $PORT
do
echo "Waiting for the $CONTAINER container..."
sleep 0.5
done
echo "$CONTAINER listening at $ADDR:$PORT"
}
wait_for_container ipercroncompose_rethinkdb_1 28015
wait_for_container ipercroncompose_rabbitmq_1 5672
cd test
pytest
问题的确确实是netcat
需要IP地址作为其第一个输入,而不是Docker容器名称。(我也将ADDR
变量设置为局部变量)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句