A
A
Alexander2016-09-30 23:57:55
Nginx
Alexander, 2016-09-30 23:57:55

How to get the real ip of the user inside the docker container?

I'm building a docker image based on "ubuntu:16.04". I raise nginx in it with the simplest configuration:

server {
  listen 80;

  location / {
    return 200 'Remote addr = ${remote_addr}, X-Forwarded-For = ${proxy_add_x_forwarded_for}';
  }
}

I raise the container:
docker run --name=mycontainer -p 80:80 -d myimage
When making requests to the server, the response is:
"Remote addr = 172.17.0.1, X-Forwarded-For = 172.17.0.1"

where "172.17.0.1" is the address from the docker internal network, not the real ip of the client that made the request.
How can I get the real ip of a user?
You can, of course, specify "--net=host" when starting the container, but this is inconvenient, because the possibility of binding the container port to an arbitrary host port is lost.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nazar Mokrinsky, 2016-10-01
@nazarpc

When asked from where? It looks like you're on the internal network and making requests, otherwise X-Forwarded-For would be different. Make a request to the public IP of the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question