I’m new to Docker and have the following problem recently.
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 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 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 request.open(‘POST’, “http://172.16.1.20:8888/somewhere”, 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.