gangadhars
gangadhars

Reputation: 2728

Get host IP in docker container

I have a ubuntu container on Ubuntu system. The container acts like client and Ubuntu host system acts like server. 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. How to get the Host Ip in container. I tried

/sbin/ip route|awk '/default/ { print $3 }'

But it returning Docker ip: 172.17.42.1. But my Host system idp is: 192.168.2.101. How to get host ip in docker container. You can see below code to know problem.

Ubuntu Host server.c looks like below:

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:

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?

Upvotes: 1

Views: 603

Answers (1)

cnicutar
cnicutar

Reputation: 182714

But my Host system idp is: 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.

There is no "standard" way to get the other addresses configured on the host; I didn't find any docker ways to do this.


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".

Upvotes: 2

Related Questions