arashffff
(Arash Alf)
April 15, 2023, 9:32pm
1
Hi, i have problem to find a requester’s ip
I have docker compose with 2 containers:
frontend
- port :
3000:3000
backend
- port :
8000:8000
When request to frontend container and then the frontend container request to backend container , the requester’s ip is a ( backend container Ip ) !
Shouldn’t it be a 127.0.0.1 ?!?
Or my system IPV4 ?!?
meyay
(Metin Y.)
April 16, 2023, 11:12am
2
A container in a bridge network can not see the source ip of a request. It will see the bridge networks gateway as source ip.
If you need to retain the source ip, the container must either use the host network or use a macvlan or ipvlan network.
This can only be true, if the container uses the host network, and you either use one of both in the URL to access the container.
arashffff
(Arash Alf)
April 16, 2023, 2:18pm
3
What is your solution for Windows os?
meyay
(Metin Y.)
April 16, 2023, 2:25pm
4
Docker Desktop for Windows? I am afraid none.
arashffff
(Arash Alf)
April 16, 2023, 2:32pm
5
Yes in windows desktop ;(
I can’t change my os and i should fix it on win
meyay
(Metin Y.)
April 16, 2023, 3:37pm
6
You could run a reverse proxy on your windows host (e.g. like nginx, apache, or traefik) and inject the X-Forwarded-For header, and make your containers read the source ip from the header.
Treafik has a binary release that could be used: Traefik Installation Documentation - Traefik + the file provider example should help you configure Traefik for your needs. The same should exist for nginx and apache.
meyay
(Metin Y.)
April 16, 2023, 4:38pm
7
Here is a poc for traefik.
Download the binary release, extract the zip and place these two files with this content in the same folder as traefik.exe.
file_provider.yml:
entryPoints:
web:
address: :80
providers:
file:
filename: ./dynamic_conf.yml
dynamic_conf.yml:
# http routing section
http:
routers:
frontend:
rule: "Host(`localhost`) && PathPrefix(`/frontend/`)"
middlewares:
- fronted-stripprefix
service: frontend
backend:
rule: "Host(`localhost`) && PathPrefix(`/backend/`)"
middlewares:
- backend-stripprefix
service: backend
middlewares:
fronted-stripprefix:
stripPrefix:
prefixes:
- "/frontend/"
backend-stripprefix:
stripPrefix:
prefixes:
- "/backend/"
services:
frontend:
loadBalancer:
servers:
- url: http://localhost:3000/
backend:
loadBalancer:
servers:
- url: http://localhost:8000/
Then run treafik with ./traefik --configfile file_provider.yml
in the folder. Then you can reach the frontend using http://localhost/fronend/
and the backend using http://localhost/backend/
.
If you modify your hosts file (c:\Windows\System32\drivers\etc\hosts
) as admin, you can add arbitrary domain names and resolve them to 127.0.0.1. Then you could use those hosts in http.router rules and remove the PathPrefix and the middleware.
Traefik should inject common x-headers by default to the forwarded request.