[英]Get host IP in docker container
I have a ubuntu container on Ubuntu system. 我在Ubuntu系统上有一个ubuntu容器。 The container acts like client and Ubuntu host system acts like server.
容器的行为类似于客户端,而Ubuntu主机系统的行为类似于服务器。 So I want to send data to my Host system by using socket programming.
所以我想通过套接字编程将数据发送到我的主机系统。 To send the data to host sytem I want host Ip.
要将数据发送到主机系统,我需要主机Ip。 How to get the Host Ip in container.
如何在容器中获取主机IP。 I tried
我试过了
/sbin/ip route|awk '/default/ { print $3 }'
But it returning Docker ip: 172.17.42.1
. 但是它返回了Docker ip:
172.17.42.1
。 But my Host system idp is: 192.168.2.101
. 但是我的主机系统idp是:
192.168.2.101
。 How to get host ip in docker container. 如何在Docker容器中获取主机IP。 You can see below code to know problem.
您可以查看以下代码来了解问题。
Ubuntu Host server.c looks like below: Ubuntu Host server.c如下所示:
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
Container client.c looks like below: 容器client.c如下所示:
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // <-- I want place my Host IP here
How to get host Ip in container? 如何在容器中获取主机IP?
But my Host system idp is: 192.168.2.101
但是我的主机系统IDP是:192.168.2.101
Your host machine probably has multiple addresses. 您的主机可能有多个地址。 It is likely you have
192.168.2.101
on eth0
or wlan0
etc. and 172.17.42.1
on docker0 - the virtual interface used to communicate with the containers. 您可能在
eth0
或wlan0
等上具有192.168.2.101
在172.17.42.1
上具有172.17.42.1-用于与容器通信的虚拟接口。
There is no "standard" way to get the other addresses configured on the host; 没有“标准”的方法来获取主机上配置的其他地址。 I didn't find any docker ways to do this.
我没有找到任何执行此操作的docker方法。
At any rate, the 172.17.42.1
address probably does 100% of what you want - however way you put it your traffic will still flow over that "interface". 无论如何,
172.17.42.1
地址可能会完成您想要的100%的地址-但是,按照您所说的方式,您的流量仍然会在该“接口”上流动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.