Unable to communicate to a mongo container from another spring boot container

I have 2 containers , one running MongoDB and the other running SpringBoot app on it. Both of them are in same network. In Spring-Config.xml and application.properties, I have specified the IP that I received by doing a bash in Mongo Container and then doing a cat /etc/hosts; however SpringBoot App still refuses to connect .

The commands to run containers are:

docker run -d --name mongocontainer --network=springbootmongo -v ~/mongo-data:/data/db mongo
docker run  my.app --network=springbootmongo 

docker ps returns

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1f82313b394a        mongo               "docker-entrypoint.s…"   32 minutes ago      Up 32 minutes       27017/tcp           mongocontainer

application.properties says:
spring.data.mongodb.uri=mongodb://172.21.0.2:27017/test

SpringConfig.xml has
<mongo:mongo-client id="mongo" host="172.21.0.2" port="27017" >

The ERROR says :

2019-11-18 06:00:03.559  INFO 1 --- [72.21.0.2:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server 172.21.0.2:27017

com.mongodb.MongoSocketOpenException: Exception opening socket
	at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-3.11.2.jar!/:na]
	at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128) ~[mongodb-driver-core-3.11.2.jar!/:na]
	at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.11.2.jar!/:na]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111-internal]
Caused by: java.net.SocketTimeoutException: connect timed out
	at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_111-internal]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111-internal]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111-internal]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_111-internal]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_111-internal]
	at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_111-internal]
	at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.11.2.jar!/:na]
	at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-3.11.2.jar!/:na]
	at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-3.11.2.jar!/:na]
	... 3 common frames omitted

I have changed mongod.service to use bind_ip_all

`

ExecStart=/usr/bin/mongod --bind_ip_all --config /etc/mongod.conf

`

Any idea what am I missing?

Managed to fix it my exclusively specifying network=mynetwork before the app image
e.g.

docker run --network=mynetwork suman

Just for reference, you don’t have to explicitly define Ip addresses. when two containers are one the same network you can use container name as hostname for connection strings. for example, you can use :
<mongo:mongo-client id="mongo" host="mongocontainer" port="27017" >
instead of :
<mongo:mongo-client id="mongo" host="172.21.0.2" port="27017" >