How to call one microservice from another microservice using docker images
Trying to communicate with the other container won't work with localhost
.
You should create a custom bridged network, which will allow you to refer to the containers by name. And there is no need to publish the ports if you are only talking internally.
# create networkdocker network create -d bridge mynet# container 1docker container run --network mynet --name container1 -d image_name# container 2docker container run --network mynet --name container2 -d some_other_image_name
The IP in code snippet can then be replaced with the name of the other container
RestTemplate restTemplate = new RestTemplate();ResponseEntity<Boolean> isUp = restTemplate.getForEntity("http://container2:2002/apis/test",Boolean.class)
Alternately, you can also link the two containers together by --link
. Assuming you want container1 as client to container2, you can use below:
sudo docker run --link container2 --name=container1 -d image_name