I’m new to Docker and have found the following problem in my work these days.
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 JavaScripts 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 a 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 serviceB's IP address) from the 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
request.open(‘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 in the http cannot be resolved. But this is weird as ping works. Till this point, both serviceA and serviceB are configured to use dynamically assigned IP addresses. Then if serviceB is changed to real IP address, such as
request.open(‘POST’, “http://172.16.1.20:8888/somewhere”, true)
then it works as intended(the created customized network, mybridge, is configured in the subnet 172.16.1.0/24). With customized docker network, I can configure static IP address, which is 172.16.1.20 in this example, for serviceB in docker-compose.yaml (while serviceA still gets a dynamic IP address) to solve this problem. The question, however, is that why it fails to work with the name under dynamic IP address while ping could work for serviceB?
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. More specifics about my docker compose configuration could be provided if required and Thanks in advance.