[英]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. Web应用程序上有一个功能,我们需要发送服务器IP,但我总是发送
172.17.0.2
因为这是request.META['REMOTE_ADDR']
的值,当在localhost中使用django时通常为127.0.0.1
我假设这是请求来自的NGINX容器的TCP地址。 How will I send the IP of my docker host instead? 我该如何发送Docker主机的IP?
Containers: 容器:
Pass it as an environment variable to your container when you create it. 创建它时,将其作为环境变量传递到您的容器。 Then, read that environment variable in your Django code.
然后,在Django代码中读取该环境变量。
You can do it with option -e HOST_IP=$(/sbin/ip route | awk '/default/ { print $3 }')
in docker run
command. 您可以在
-e HOST_IP=$(/sbin/ip route | awk '/default/ { print $3 }')
docker run
命令中使用选项-e HOST_IP=$(/sbin/ip route | awk '/default/ { print $3 }')
来docker run
。
In docker-compose.yml
, you could do something like this. 在
docker-compose.yml
,您可以执行以下操作。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.