mongodbspring-bootdockerspring-data-mongodbunknown-host

SpringBoot in Docker not connecting to Mongo on server (Non-Dockerized)


I'm getting following error while trying to connect to mongodb cluster - ReplicaSet (non-dockerized) deployed on three different server via a dockerized spring boot application.

2020-04-06 19:40:06.263  INFO 1 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[196.12.7.156:27017, 196.12.7.157:27017, 196.12.7.158:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2020-04-06 19:40:06.263  INFO 1 --- [           main] org.mongodb.driver.cluster               : Adding discovered server 196.12.7.156:27017 to client view of cluster
2020-04-06 19:40:06.301  INFO 1 --- [           main] org.mongodb.driver.cluster               : Adding discovered server 196.12.7.157:27017 to client view of cluster
2020-04-06 19:40:06.304  INFO 1 --- [           main] org.mongodb.driver.cluster               : Adding discovered server 196.12.7.158:27017 to client view of cluster
2020-04-06 19:40:06.424  INFO 1 --- [.16.8.179:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:33289}] to 196.12.7.158:27017
2020-04-06 19:40:06.427  INFO 1 --- [.16.8.179:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=196.12.7.158:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 11]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=1686431, setName='rs0', canonicalAddress=seabatch3:27017, hosts=[seabatch3:27017, seabatch1:27017, seabatch2:27017], passives=[], arbiters=[], primary='seabatch1:27017', tagSet=TagSet{[]}, electionId=null, setVersion=6, lastWriteDate=null, lastUpdateTimeNanos=2764696590071705}
2020-04-06 19:40:06.433  INFO 1 --- [.16.8.177:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:75063}] to 196.12.7.156:27017
2020-04-06 19:40:06.434  INFO 1 --- [.16.8.178:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:24932}] to 196.12.7.157:27017
2020-04-06 19:40:06.435  INFO 1 --- [.16.8.179:27017] org.mongodb.driver.cluster               : Discovered cluster type of REPLICA_SET
2020-04-06 19:40:06.438  INFO 1 --- [.16.8.177:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=196.12.7.156:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 11]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=4744734, setName='rs0', canonicalAddress=seabatch1:27017, hosts=[seabatch3:27017, seabatch1:27017, seabatch2:27017], passives=[], arbiters=[], primary='seabatch1:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000061, setVersion=6, lastWriteDate=null, lastUpdateTimeNanos=2764696601428299}
2020-04-06 19:40:06.438  INFO 1 --- [.16.8.178:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=196.12.7.157:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 11]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=null, roundTripTimeNanos=3873250, setName='rs0', canonicalAddress=seabatch2:27017, hosts=[seabatch3:27017, seabatch1:27017, seabatch2:27017], passives=[], arbiters=[], primary='seabatch1:27017', tagSet=TagSet{[]}, electionId=null, setVersion=6, lastWriteDate=null, lastUpdateTimeNanos=2764696602199597}
2020-04-06 19:40:06.441  INFO 1 --- [.16.8.179:27017] org.mongodb.driver.cluster               : Adding discovered server seabatch3:27017 to client view of cluster
2020-04-06 19:40:06.447  INFO 1 --- [.16.8.179:27017] org.mongodb.driver.cluster               : Adding discovered server seabatch1:27017 to client view of cluster
2020-04-06 19:40:06.455  INFO 1 --- [.16.8.179:27017] org.mongodb.driver.cluster               : Adding discovered server seabatch2:27017 to client view of cluster
2020-04-06 19:40:06.461  INFO 1 --- ['}-seabatch3:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server seabatch3:27017

com.mongodb.MongoSocketException: seabatch3: Name does not resolve
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211) ~[mongodb-driver-core-3.11.2.jar!/:na]
    at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75) ~[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]
    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:748) [na:1.8.0_212]
Caused by: java.net.UnknownHostException: seabatch3: Name does not resolve
    at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_212]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929) ~[na:1.8.0_212]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1277) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_212]
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-3.11.2.jar!/:na]
    ... 5 common frames omitted

2020-04-06 19:40:06.466  INFO 1 --- ['}-seabatch1:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server seabatch1:27017

com.mongodb.MongoSocketException: seabatch1: Name does not resolve
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211) ~[mongodb-driver-core-3.11.2.jar!/:na]
    at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75) ~[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]
    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:748) [na:1.8.0_212]
Caused by: java.net.UnknownHostException: seabatch1: Name does not resolve
    at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_212]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929) ~[na:1.8.0_212]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1277) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_212]
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-3.11.2.jar!/:na]
    ... 5 common frames omitted

2020-04-06 19:40:06.466  INFO 1 --- [.16.8.179:27017] org.mongodb.driver.cluster               : Canonical address seabatch3:27017 does not match server address.  Removing 196.12.7.158:27017 from client view of cluster
2020-04-06 19:40:06.474  INFO 1 --- [.16.8.178:27017] org.mongodb.driver.cluster               : Canonical address seabatch2:27017 does not match server address.  Removing 196.12.7.157:27017 from client view of cluster
2020-04-06 19:40:06.474  INFO 1 --- ['}-seabatch2:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server seabatch2:27017

com.mongodb.MongoSocketException: seabatch2: Name does not resolve
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211) ~[mongodb-driver-core-3.11.2.jar!/:na]
    at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75) ~[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]
    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:748) [na:1.8.0_212]
Caused by: java.net.UnknownHostException: seabatch2: Name does not resolve
    at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_212]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929) ~[na:1.8.0_212]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1277) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_212]
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-3.11.2.jar!/:na]
    ... 5 common frames omitted

Initially, it connects to the mongodb cluster and get the hostnames and then application fails to start.

My guess is that since app deployed in docker container, docker is assume that mongodb is also deployed in another container and trying to find it via container names.

Any suggestion on how I can resolve this issue would help alot.

Thank you!


Solution

  • In the replica set configuration, each node is referred by some host name. These host names need to be resolvable and accessible from your application.

    It sounds like you are using names that make sense on the host but are not resolvable in your container. From your container you have some other host name you are using in your connection string. The initial connection then succeeds but the driver then reconnects to each RS member using the names defined in the RS configuration, and this fails.

    You need to either change RS configuration to use names that are resolvable both from the host and from the container, or make your existing names resolvable and accessible from the container.

    "Accessible" means if the names resolve to different IP addresses in different environments, those IPs must route to the nodes in question.