Docker communication between apps in separate containers
I have been looking everywhere for this answer. To me it seems like an obvious question, however, the answer has eluded me.
My current setup is, I have redis, mongodb and two api servers on the same bridge network. The first server serves as a gateway api that does all the auth, and exposes certain api calls. The backend api is the one that handles all the db interactions and data munging. If I hit the backend (inner) api alone, I am able to see the contents (this api would not be exposed in real production environment). However, if I make the same request from within the gateway api, I am not able to hit the backend (inner) api that is also part of the bridged network I created.
Below is a diagram of the container interactions.
One Solution collect form web for “Docker communication between apps in separate containers”
I still use legacy linking, but I’m a little bit familiar with this. I think the problem is that you are trying to hit “localhost” from inside your gateway container. The inner API container cannot be resolved as “localhost” inside of the gateway API container. You are able to hit “localhost:8099” from the host machine or externally because of the port mapping, but none of your other containers will be able to resolve that address/port because they ‘think’ it’s a remote machine.
Here’s a way to test what I’m thinking. In your host’s shell, run the bridge inspect command shown here. Copy the IP address from
IPV4. Then open a shell in the gateway container with
docker exec -it <gateway-id> /bin/bash and then use
wget to see if you can hit that IP address you copied.
If my thinking is correct, you will see that you must use your inner-API node’s Docker assigned IP address from the other containers. Amongst other options, you can start containers with a static IP address as shown here.
This is starting to escape the scope of my knowledge, but you can also configure a container DNS. Configure container DNS.