I am using --add-host to map the host machine to a hostname in a docker container.
From inside the container, I can only access servers on the host machine who bind to an IPv4 address, but not those who bind to an IPv6 address.
How to reproduce
Run IPv4 server on port 8004:
python3 -m http.server 8004
Run IPv6 server on port 8006:
python3 -m http.server 8006 --bind ::
By now, netstat -tl | grep :800 should show the following:
tcp 0 0 0.0.0.0:8004 0.0.0.0:* LISTEN
tcp6 0 0 [::]:8006 [::]:* LISTEN
Now run the following command:
docker run -it --add-host=outside:host-gateway alpine:latest sh
This would open a pseudo terminal into a docker container (running alpine image). Attemting to read the IPv6-bound server, as follows:
wget http://outside:8006
fails with an error message "wget: can't connect to remote host: Connection refused".
However, attemting to reach the IPv4-bound server
wget http://outside:8004
works, and an index.html file is dowloaded.
The real problem
In my application I'm actually using extra_hosts option in a docker-compose, but the result is the same. I chose to use docker in my minimal example for simplicity.
I had to "downgrade" my server (Spring Boot Tomcat) to use IPv4 binding only in order to get things working properly.
Is it a bug? Is there a way to access IPv6-bound servers from within a docker container?
Docker version is 27.5.1.