Cross container communication via HTTP POST request

I’m new to Docker and have the following problem recently.

Basically, I have defined two services, for instance, serviceA and serviceB, in docker-compose.yaml and used docker-compose to start these two containers at the same time. Specifically, serviceA is a node.js environment with some JavaScript work written by myself. In addition, there is also a webpack-dev-server in this container for development purpose. serviceB is configured as a python-tornado web server with some self-defined logic to handle HTTP POST request sent from serviceA.

I have created customized bridge network with the help of docker network. Assume this network is with name mybridge. Then, in the docker-compose.yaml, the two services are both defined to use this customized network. Now, after starting these two services with docker-compose, I can successfully ping serviceB (with name, not IP address) from container of serviceA, and vice versa. But the problem arises when sending HTTP POST request using JS from serviceA. Generally, I just initialized a new request with XMLHttpRequest (request=new XMLHttpRequest();) , then‘POST’, “http://serviceB:8888/somewhere”, true). In this case, I get Failed to load resource: net::ERR_NAME_NOT_RESOLVED error in my chrome browser. My guess is that the name serviceB cannot be resolved. But this is weird as ping works. Till now, both serviceA and serviceB are configured to use dynamically assigned IP addresses. Then if serviceB is changed to real IP address, like‘POST’, “”, true), then it works as intended. With customized docker network, I can configure static IP address for serviceB in docker-compose.yaml to solve this problem. The question, however, is that why it fails to work with name under dynamic IP address while ping could work?

I googled a lot for this problem during the past few days and failed to find a proper solution. Thus, I created a post here in the hope that someone could provide a helping hand on this problem. Thanks in advance.

1 Like

is it posible for you to share your docker-compose file?

Here goes my docker-compose.yaml with some project-related details omitted, but the general structure is there (I’m sorry for the poor format of it, still learning how to format yaml more properly :frowning:

version: "3"
image: serviceB:0.0.1
stdin_open: "true"
tty: "true"
  - "8888"
  - /dev/fuse:/dev/fuse
command: "bash -c 'cd /home/xyz; python'"

image: serviceA:0.0.1
  - mybridge
  - "49160:8080"
  external: true

And the customized network mybridge is created beforehand with the command docker network create --gateway --subnet mybridge

This script uses static IP address for serviceB which works well. If the line ipv4_address: is commented and use dynamic IP, then serviceA will fail to send request based on name of serviceB with aforementioned error.

1 Like

Since you created the custom network beforehand, then you need to specify the custom network in your docker-compose file as external. Added a new network section (same level as your service A and B) like this:

name: mybridge

You can add this “networks” section before after the “version” and before “services”.

i have the exact same problem. i cant access node express server from nginx web server , which request with service name. example http://api:3000/pharmacyshops doesnt work. i have a workaround with static ip but why not work with service name idk. (i can curl inside the nginx container with servicename that works too.)