我想获取容器的IP地址,以便将IP地址设置为环境变量。
搬运工-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需要:
一个。 set --liveserver
python /app/manage.py test --liveserver=django-appnet-ip:port
B中。或者我设置环境变量:
DJANGO_LIVE_TEST_SERVER_ADDRESS=django-appnet-ip:port
问题是,在创建容器之前,不设置docker容器的ip地址。那么如何将IP地址传递给django?
到目前为止我尝试了什么......
一个。创建一个调用脚本然后调用管理命令的django管理命令:
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-compose:
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地址传递给容器,以便容器运行的命令获取IP地址?
或许对这个问题采取完全不同的方法?
答案 0 :(得分:3)
我创建了一个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"
可能更好的设置环境变量,以便您可以在其他地方使用它。
答案 1 :(得分:0)
这是容器依赖问题吗?你在容器里运行硒吗? docker-compose
建议使用2个或更多的docker容器,但是你只引用了Django容器。如果这是一个容器依赖问题,那么遗憾的是,目前不可能使用docker-compose
先在另一个容器之前启动一个容器。但是你可以使用......
docker wait
...在bash脚本中执行命令然后使用...
docker inspect containerName
...获取IP地址并将该IP传递给docker撰写。
docker-compose
支持使用变量。