简体   繁体   English

Docker容器端口立即不可用

[英]Docker container ports not available right away

I have Jenkins to build my project and run my unit test and integration test. 我让Jenkins构建我的项目,并运行我的单元测试和集成测试。 Before all this, Jenkins is launching several docker containers (with mongodb , cassandra etc inside) and then it starts. 在此之前, Jenkins将启动几个docker容器(其中包含mongodbcassandra等),然后启动。 Sometimes, my tests fail because the they cannot reach the resources. 有时,我的测试失败,因为它们无法到达资源。 After digging a bit I noticed that not all my containers start. 挖掘了一下之后,我发现并不是我的所有容器都启动了。 So here is what I have: 所以这是我所拥有的:

docker-compose.yml with the definition of each docker container docker-compose.yml与每个定义docker容器

start_docker.sh script: start_docker.sh脚本:

    time docker-compose pull
    time docker-compose --project-name $JOB_NAME up -d
    time docker-compose --project-name $JOB_NAME ps
    echo "Wait services are started"

    docker-compose --project-name $JOB_NAME ps -q
    container_names=`docker-compose --project-name $JOB_NAME ps -q`
    container_nb="${#container_names}"
    for container_name in $container_names; do

        ports=`docker port $container_name | cut -d "/" -f1 `
        service_ip=`docker inspect $container_name | grep "IPAddress" | grep 172 | sed "s/[^0-9]*\\([0-9\\.]\\+\\)[^0-9]*/\\1/"`

        for port in $ports; do
            while ! nc -z $service_ip $port; do
                sleep 1 # wait for 1 second before check again
            done
        done
    done

What I've noticed is that sometimes the ports variable is empty for some containers and if I'm displaying them here is what I see: 我注意到的是,有时某些容器的ports变量为空,如果我在此处显示它们,这是我所看到的:

[unit_test] target2sellcoredevelop_dse_1                     /etc/dse/run.sh                  Up                             
[unit_test] target2sellcoredevelop_mongo_1                   docker-entrypoint.sh mongo ...   Up                             
[unit_test] target2sellcoredevelop_pentaho-pdi_1             /bin/sh -c /bin/bash -c "/ ...   Up      0.0.0.0:9999->9999/tcp 
[unit_test] target2sellcoredevelop_rabbitmq_1                docker-entrypoint.sh /init.sh    Up                             
[unit_test] target2sellcoredevelop_redis_sentinel1_1         docker-entrypoint.sh sh -c ...   Up                             
[unit_test] target2sellcoredevelop_rediscachereco_master_1   docker-entrypoint.sh redis ...   Up                             
[unit_test] target2sellcoredevelop_rediscatalog_master_1     docker-entrypoint.sh redis ...   Up                             
[unit_test] target2sellcoredevelop_redisuser_master_1        docker-entrypoint.sh redis ...   Up

As you can see some of them already have the port number and some of them don't. 如您所见,其中有些已经具有端口号,而有些则没有。 I have 2 problems: 我有2个问题:

  1. Why the port number is not available? 为什么端口号不可用? Is because some docker zombie process? 是因为某些Docker僵尸进程吗? If so, how I can clean the up? 如果是这样,我该如何清理?

  2. How can I properly wait for all them to really start? 我怎样才能正确地等待所有的人真正开始?

EDIT: I think my problem is that sometimes some containers does not start. 编辑:我认为我的问题是某些容器有时无法启动。 My question: how do I investigate that ? 我的问题:我该如何调查?

You have a number of different questions, but in Docker Compose, you can wait for certain containers to come up before others using dadarek/wait-for-dependencies . 您有许多不同的问题,但是在Docker Compose中,您可以使用dadarek / wait-for-dependencies等待某些容器出现在其他容器之前。

1). 1)。 Add a new service to your docker-compose.yml 向您的docker-compose.yml添加新服务

  waitfordb:
    image: dadarek/wait-for-dependencies
    depends_on:
      - mongodb 
    command: mongodb:27017

2). 2)。 Add the following config to the service that requires MongoDB to be up. 将以下配置添加到需要启动MongoDB的服务中。 This will essentially wait for MongoDB to be fully up. 这实际上将等待MongoDB完全启动。

depends_on: 
  - waitfordb

3). 3)。 Startup compose 启动撰写

docker-compose run --rm waitfordb
docker-compose up -d <SERVICE_1> <SERVICE_2>

This may or may not resolve your other issues, but might be a good place to start. 这可能会或可能不会解决您的其他问题,但可能是一个不错的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM