Get the host IP from my django app container

There is a feature on our web app that we need to send the IP of our server but I always send 172.17.0.2 because that is the value of request.META['REMOTE_ADDR'] which is usually 127.0.0.1 when using django in localhost and which I assume is the TCP address of our NGINX container which where the request is coming from. How will I send the IP of my docker host instead?

Containers:

  • Docker: split mysql databases into different data volume containers?
  • Why Docker on Virtualbox can't find my DockerFile?
  • nginx load balancer IP-requester based
  • Jenkins run job in local docker image
  • How to run another docker container from application inside a docker container?
  • Create container from local file
    • Nginx
    • Django with gunicorn
    • PostgreSQL
    • Redis

  • connecting APIMAN to an external ElasticSearch database for metrics
  • Node Prerender Working on First Page Load - 500 Error on Subsequent Loads
  • Mounted volume not working when linked
  • How to install pdo driver in php docker image?
  • Connection refused error with cassandra and docker-py
  • Get or set env variable in docker-compose.yml file
  • 2 Solutions collect form web for “Get the host IP from my django app container”

    Pass it as an environment variable to your container when you create it. Then, read that environment variable in your Django code.

    You can do it with option -e HOST_IP=$(/sbin/ip route | awk '/default/ { print $3 }') in docker run command.

    In docker-compose.yml, you could do something like this.

    django:
      environment:
        - HOST_IP=$(/sbin/ip route | awk '/default/ { print $3 }')
    

    Try this:

    import subprocess
    host = subprocess.check_output(['bash', '-c', "/sbin/ip route|awk '/default/ { print $3 }'"]).decode('utf-8').strip()
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.