Docker Community Forums

Share and learn in the Docker community.

Cross container communication via HTTP POST request


(Gerryken) #1

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 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.


(Martin Terp) #2

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


(Gerryken) #3

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"
services:
serviceB:
image: serviceB:0.0.1
networks:
  mybridge:
    ipv4_address: 172.16.1.20
stdin_open: "true"
tty: "true"
expose:
  - "8888"
volumes:
  - /dev/fuse:/dev/fuse
command: "bash -c 'cd /home/xyz; python start.py'"

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

And the customized network mybridge is created beforehand with the command docker network create --gateway 172.16.1.1 --subnet 172.16.1.0/24 mybridge

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