1

I have a docker-compose that spins up 2 Docker containers. One is an application (on port:8090) and the other is a database (on port:5432). I also have a Windows application that has an API accessible through localhost:8002. I want to use my container that has the application to read data from the localhost:8002 API, then move that data to my database in my other Docker container.

For docker-compose, I mapped port 5432:5432 and port 8090:8090 for the database and application containers, respectively. I have tested my application non-dockerized where I call the Windows API and then write it to port:5432 and it works properly.

However, after Dockerizing my application, localhost:8002 is now no longer localhost for my Dockerized application and is now unreachable. I am wondering how I can reach my host localhost:8002, hit that API, then move that data to my other container.

Rui Nian
  • 2,544
  • 18
  • 32
  • It depends on how you have your networking set up. By default, you get a bridged network. If you `ifconfig` in your container, the gateway address should be the locally-known IP address of your host. – Tim Roberts Mar 29 '22 at 20:56
  • Hi Tim, I assume you mean `ipconfig`. I have indeed tried this and feel like this is the answer, however, there are so many IPv4 ports I can use. Which one would I use? I have: `Ethernet adapter vEthernet`, `Ethernet adapter vEthernet (WSL)`, `VMWare Networks`, `Wireless LAN adapter Wi-Fi`, `Ethernet adapter vEthernet (nat)`. – Rui Nian Mar 29 '22 at 21:00
  • If you do `route print`, there should only be one gateway. Hopefully. – Tim Roberts Mar 29 '22 at 21:06
  • That sounds like the problem described in [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach); do the answers there help you? (On a Windows host, try using the special host name `host.docker.internal` to connect back to the host.) – David Maze Mar 29 '22 at 21:10
  • Hey David, I actually tried that as well with `http://host.docker.internal:8002` and `host.docker.internal:8002`. For whatever reason, that did not work either and I had no idea why. Perhaps I am specifying the function incorrectly, I will explore this further. Also for Tim, when i ran a `route print`, I actually got about 35 routes in the IPv4 Route Table :(. – Rui Nian Mar 29 '22 at 21:30

1 Answers1

1

After 16 hours of blood, sweat, and tears. Mostly tears, here's the answer for whoever may be using this in the future.

TL;DR expose your local nat network to your docker containers in docker-compose by:

networks:
  default:
    external: true
    name: nat

Note, on Windows, running a docker network ls should give something like:

NETWORK ID     NAME             DRIVER    SCOPE
6b30a7dcf6e0   Default Switch   ics       local
26305680ad62   WSL              ics       local
b52f5e497eba   nat              nat       local
4a4fd550398f   none             null      local

The docker-compose is simply connecting the docker network to your host network (required for auto-restart of containers after computer restarts as well, but off-topic).

Afterwards, I had to run an ipconfig to find the IPv4 port that corresponds to my nat port. Here, there can be many IPv4 IP addresses, find the one that says Ethernet adapter vEthernet (nat).

Then, use the IPv4 Address corresponding to the nat network for any applications running on your local machine.

For example, my application ran on localhost:8002. Here, I changed my host to http://172.31.160.1:8002 and it worked.

Rui Nian
  • 2,544
  • 18
  • 32