Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST request from one docker container to another fails

I have two applications, one of which has a RESTful interface that is used by the other. Both are running on the same machine.

Application A runs in a docker container. I am running it using the command line:

docker run -p 40000:8080 --name AppA image1

When I test Application B outside a docker container (in other words, before it is dockerized) Application B successfully executes all RESTful requests and receives responses without problems.

Unfortunately, when I dockerize and run Application B within a container:

docker run -p 8081:8081 --name AppB image2

whenever I attempt to send a RESTful request to Application A, I get the following:

Connect to localhost:40000 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused

Of course, I also tried making Application B connect using my machine's IP address. When I do that, I get the following failure:

Connect to 192.168.1.101:40000 failed: No route to Host

Has anyone seen this kind of behavior before? What causes an application that communicates perfectly well with another dockerized application outside a docker container to fail to communicate with that same dockerized application once it is itself dockerized???

Someone please advise...

like image 864
Factor Three Avatar asked Apr 20 '16 00:04

Factor Three


1 Answers

Simply linking B to A docker run -p 8081:8081 --link AppA --name AppB image2, then you can access the REST service using AppA:8080.

The reason is that Docker containers run on its own subnet (normally 172.17.0.0-255) and they cannot access the network that your host is on. Also localhost would be the container itself, not the host.

like image 183
Xiongbing Jin Avatar answered Nov 10 '22 01:11

Xiongbing Jin