Getting real IP inside container

Hello.

So, I would like to run a service inside container which wil be accessible from outside (imagine like httpd). Everything works fine, but the IP of the “visitor” will always be 172.17.42.1. I tried bridged and host mode and it’s the same.

I searched everywhere, but I could not find a single article about this issue …

My question is, what do I have to do, so the service inside container sees the real IP not the host IP?

Thanks

PS: Docker is running on a server that is in the same network as my PC, but different device (like 192.168.1.20 and 192.168.1.30). The issue is also no different, if I access container from outside (via port forwarding).

without much knowledge about it I think the default way is to assume the hosts IP address plus a port number that can be assigned randomly or fixed, so you reach your container’s service under xxx.xxx.xxx.x:yy

this might also be a god read https://docs.docker.com/v1.11/engine/userguide/networking/dockernetworks/

I think you have misunderstood my issue.

For example I run Apache in container. When I access it for example with 192.168.1.60:8080 (IP of docker host in my network), the container sees me as 172.17.42.1, when it should display 192.168.1.70 (my PC) or my external IP, if I access it externally (eg. 175.68.25.69).

Second example. I run SSH inside container and I run command “who” from the client. Instead of getting my real IP, I get 172.17.42.1.

I tried bridge and host most and the thing is the same.

Okay I found out, if I use --net=host then I get real visitor IPs. However, I would prefer using bridge mode, so I do not “contaminate” host with all ports that I don’t even need to be exposed outside of the container.

Is it possible to achieve that somehow?

Hi,

(I’m replying to this old thread because somebody on the mailing list said they recently had a similar issue.)

Docker should report the real visitor IP address. For instance. this is a log line coming from a web server on one of my EC2 instances:

180.76.15.146 - - [11/Feb/2017:14:32:34 +0000] "GET / HTTP/1.1" 200 728 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" "-"

The server is using the official nginx image, without any kind of special customization.

I would suggest:

  • check your iptables rules (are you using any custom rule that might NAT the traffic?)
  • make sure you have a recent version of Docker (your initial post didn’t indicate the version that you’re using, and the version of the OS)

Note that in some scenarios (e.g. Docker Mac), there might be an extra translation layer hiding the real IP address of the client. If you want to know why, check this other post for a detailed explanation of Docker Mac’s VPNKit network layer.

I hope this helps.