I have 2 docker containers running on my host, web1 and web2. Container web1 maps port 8080 to the host port 18080, and web2 maps port 8080 to the host port 28080. The IP address of the host is 192.168.20.111 (yes it's LAN). I can access 192.168.20.111:18080 and 192.168.20.111:28080 from other machines in the same LAN.
When I tried to access 192.168.20.111:18080 (curl 192.168.20.111:18080) or 192.168.20.111:28080 from within web1/web2 container, I got a timeout error.
However, the Apache server running on port 8080 of the host can be accessed by 192.168.20.111:8080 from within web1/web2 container, which means the route from container to host is clear.
So my question is: Why the timeout error happens and how to access 192.168.20.111:18080 from docker container?
This is the docker-compose file I used to start up web2 (web1 is almost the same):
version: '2'
services:
web:
build: .
ports:
- "28080:8080"
expose:
- "8080"
environment:
- TALENTS_AUTH_HOST=192.168.20.111
- TALENTS_AUTH_PORT=18080
- TALENTS_ANALYSIS_HOST=192.168.20.111
- TALENTS_ANALYSIS_PORT=18082
And this is the output of curl:
root@ea49393e56a4:/# curl -v http://192.168.20.111:28080
* Rebuilt URL to: http://192.168.20.111:28080/
* Trying 192.168.20.111...
* TCP_NODELAY set
* connect to 192.168.20.111 port 28080 failed: Connection timed out
* Failed to connect to 192.168.20.111 port 28080: Connection timed out
* Closing connection 0
curl: (7) Failed to connect to 192.168.20.111 port 28080: Connection timed out
root@ea49393e56a4:/#
This is ip route of container web2:
root@ea49393e56a4:/# ip route
default via 172.25.0.1 dev eth0
172.25.0.0/16 dev eth0 proto kernel scope link src 172.25.0.2
root@ea49393e56a4:/#
This is the output of curl using gateway ip:
root@ea49393e56a4:/# curl -v http://172.25.0.1:28080
* Rebuilt URL to: http://172.25.0.1:28080/
* Trying 172.25.0.1...
* TCP_NODELAY set
* connect to 172.25.0.1 port 28080 failed: Connection timed out
* Failed to connect to 172.25.0.1 port 28080: Connection timed out
* Closing connection 0
curl: (7) Failed to connect to 172.25.0.1 port 28080: Connection timed out
root@ea49393e56a4:/#
And by the way, the host machine(192.168.20.111) is a VirtualBox virtual machine running Ubuntu-16.04, and it's hosted in a windows-10 desktop, using bridge network to connect to the LAN.