My Spring boot app is able to connect to Mongo DB and running fine as standalone app.
However, if the same app is set to run in Docker, the DB connection fails with exception ‘java.net.ConnectException’
My Setup :
Spring boot App is running in docker, Mongo is outside of Docker.
Spring boot version : 2.7.4
Java Version : 11
Mongo DB version : v4.2.21 ( Running outside of Docker). Local and Stage environments have standalones installations with replicaSet.(PROD is more comprehensive)
mongodb URL from application.properties
spring.data.mongodb.uri=mongodb://host.docker.internal,localhost,127.0.0.1
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=xxxxxxx
spring.data.mongodb.replica-set-name=myreplicaset
ClusterDescription when running as Standalone {type=REPLICA_SET, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=127.0.0.1:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=13735215, setName=‘myreplicaset’, canonicalAddress=localhost:27017, hosts=[localhost:27017], passives=, arbiters=, primary=‘localhost:27017’, tagSet=TagSet{}, electionId=7fffffff0000000000000028, setVersion=1, topologyVersion=null, lastWriteDate=Tue Dec 13 16:29:48 EST 2022, lastUpdateTimeNanos=3150768696200}]}
ClusterDescription when running inside a Docker Container {type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]}
pom.xml entries
org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-data-mongodbCan anyone tell me the reason for connection issues?
Thanks in advance,
Gana