I want to get the IP address of a container so that I can set that IP address as an environmental variable.
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"
The problem is that in order to run correctly django needs either:
A. set --liveserver
python /app/manage.py test --liveserver=django-appnet-ip:port
B. or I set environmental variable:
DJANGO_LIVE_TEST_SERVER_ADDRESS=django-appnet-ip:port
Problem is that the ip address for the docker container isn't set until the container is created. So how can I pass the IP address to django?
What I have tried so far...
A. Creating a django management command that calls the script that then calls a management command:
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. Referring to the app itself in DJANGO_LIVE_TEST_SERVER_ADDRESS 'django:8081' only works if in docker-compose with this 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
If I set the command to blank, then from the command line run:
docker-compose run django python /app/project/manage.py test
I get
======================================================================
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
----------------------------------------------------------------------
The Question
How can I pass a container's ip address it a network to the container so that the command the container runs gets the IP address?
Or maybe there is a completely different approach to this problem?