将docker箱容器IP地址传递给容器

我想获取一个容器的IP地址,以便我可以将该IP地址设置为环境variables。

泊坞窗,compose.yml

django: build: . command: python /app/project/manage.py test --liveserver=172.18.0.4:8081 //hard coded - trying to avoid this ports: - "8000:8000" - "8081:8081" selenium: container_name: selenium image: selenium/standalone-firefox-debug:2.52.0 ports: - "4444:4444" - "5900:5900" 

问题是为了正确运行django需要:

A. set –liveserver

 python /app/manage.py test --liveserver=django-appnet-ip:port 

B.或者我设置环境variables:

  DJANGO_LIVE_TEST_SERVER_ADDRESS=django-appnet-ip:port 

问题是在创build容器之前,没有设置泊坞窗容器的IP地址。 那么如何将IP地址传递给django呢?

我到目前为止所尝试的…

A.创build一个调用pipe理命令的djangopipe理命令:

 class Command(BaseCommand): def add_arguments(self, parser): // I would have to redefine all the arguments because //I can't call super on django/core/management/commands/test.py ... 

B.引用DJANGO_LIVE_TEST_SERVER_ADDRESS中的应用程序本身'django:8081'只有在docker中才能使用 – 使用这个configuration:

 django: build: . net: appnet command: python /app/project/manage.py test ports: - "8000:8000" - "8081:8081" environment: - DJANGO_LIVE_TEST_SERVER_ADDRESS=django:8081 # where django is the name of the app 

如果我将命令设置为空白,然后从命令行运行: docker-compose run django python /app/project/manage.py test我得到

 ====================================================================== ERROR: setUpClass (django_behave.runner.DjangoBehaveTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/local/lib/python2.7/site-packages/django/test/testcases.py", line 1354, in setUpClass raise cls.server_thread.error error: [Errno 99] Cannot assign requested address ---------------------------------------------------------------------- 

问题

我怎样才能将一个容器的IP地址通过networking传递给容器,以便容器运行的命令获得IP地址?

或者也许有一个完全不同的方法来解决这个问题?

我做了一个bash脚本来获取docker容器id,然后使用该id从容器的/ etc / hosts中获取该容器id的ip地址。

在Django的Dockerfile

 ENTRYPOINT ["/entrypoint.sh"] 

entrypoint.sh

 #!/bin/bash set -e # Now we need to get the ip address of this container so we can supply it as an environmental # variable for django so that selenium knows what url the test server is on # only add if 'manage.py test' in the args if [[ "'$*'" == *"manage.py test"* ]] then # get the container id THIS_CONTAINER_ID_LONG=`cat /proc/self/cgroup | grep 'docker' | sed 's/^.*\///' | tail -n1` # take the first 12 characters - that is the format used in /etc/hosts THIS_CONTAINER_ID_SHORT=${THIS_CONTAINER_ID_LONG:0:12} # search /etc/hosts for the line with the ip address which will look like this: # 172.18.0.4 8886629d38e6 THIS_DOCKER_CONTAINER_IP_LINE=`cat /etc/hosts | grep $THIS_CONTAINER_ID_SHORT` # take the ip address from this THIS_DOCKER_CONTAINER_IP=`(echo $THIS_DOCKER_CONTAINER_IP_LINE | grep -o '[0-9]\+[.][0-9]\+[.][0-9]\+[.][0-9]\+')` # add the port you want on the end # Issues here include: django changing port if in use (I think) # and parallel tests needing multiple ports etc. THIS_DOCKER_CONTAINER_TEST_SERVER="$THIS_DOCKER_CONTAINER_IP:8081" echo "this docker container test server = $THIS_DOCKER_CONTAINER_TEST_SERVER" export DJANGO_LIVE_TEST_SERVER_ADDRESS=$THIS_DOCKER_CONTAINER_TEST_SERVER fi eval "$@" 

或者你可以使用类似的东西

  "$@ --liveserver=$THIS_DOCKER_CONTAINER_TEST_SERVER" 

设置环境variables可能更好,所以你可以在其他地方使用它。

这是一个容器依赖问题? 你在容器中运行selenium? docker-composebuild议2个或多个docker容器,但是你只提到Django容器。 如果这是一个容器依赖问题,那么现在就不可能使用docker-compose来先启动一个容器。 你可以使用…

 docker wait 

…命令在一个bash脚本中,然后使用…

 docker inspect containerName 

…获取IP地址并将该IP传递给docker撰写。

docker-compose DOES支持使用variables。