【发布时间】:2016-04-13 08:38:32
【问题描述】:
我想获取容器的 IP 地址,以便可以将该 IP 地址设置为环境变量。
docker-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.设置 --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呢?
到目前为止我所尝试的...
A.创建调用脚本的 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 地址?
或者也许有一个完全不同的方法来解决这个问题?
【问题讨论】:
-
如何创建一个可以在容器中运行的 shell 脚本包装器。它将有 2 个步骤。第 1 步:获取容器 ip 地址(类似于 ip addr 或 ifconfig 并从输出中解析 ip) 第 2 步:运行 django(以 ip 地址作为参数)。然后在启动容器时只需运行该外壳包装器..
-
@calvix 成功! - 我会写下来。谢谢:)