Title - MongoTimeoutException in Testcontainers Integration Test : Open Liberty app attempts to connect to Kubernetes FQDNs instead of local alias
I am writing integration tests for an Open Liberty-based microservice using Testcontainers (Java). The application is designed to run in Kubernetes with a MongoDB Replica Set (3 nodes).
When running the tests locally, the application fails to start or connect to the database because it insists on resolving the production Kubernetes FQDNs (e.g., mongodb-0.mongodb-headless...) instead of using the local Testcontainers alias I provided.
The Setup: I am manually managing the container lifecycle in @BeforeAll to ensure the MongoDB container starts first, allowing me to fetch its internal IP and attempt DNS mapping.
Test Code (ServiceEndpointIT.java):
Java
public class ServiceEndpointIT {
public static Network network = Network.newNetwork(); public static MongoDBContainer mongoDbContainer; public static GenericContainer<?> openLibertyContainer; @BeforeAll public static void setup() { // 1. Start MongoDB mongoDbContainer = new MongoDBContainer("mongo:6.0") .withNetwork(network) .withNetworkAliases("testmongo") .withExposedPorts(27017); mongoDbContainer.start(); // 2. Get Internal IP for DNS mapping String mongoIp = mongoDbContainer.getContainerInfo() .getNetworkSettings().getNetworks().values().iterator().next().getIpAddress(); // 3. Start Open Liberty openLibertyContainer = new GenericContainer<>("icr.io/my-repo/my-app:latest") .withNetwork(network) .withExposedPorts(9080) // Attempting to override connection details via Env Vars .withEnv("MONGO_HOSTS", "testmongo:27017") .withEnv("MONGO_REPLICASET", "rs0") .withEnv("MONGO_REPLICAS", "0") // Trying to force single node mode // Attempting to map the K8s Hostnames to the local Mongo IP .withExtraHost("mongodb-0.mongodb-headless.ibm-storage-defender-dev.svc.cluster.local", mongoIp) .withExtraHost("mongodb-1.mongodb-headless.ibm-storage-defender-dev.svc.cluster.local", mongoIp) .withExtraHost("mongodb-2.mongodb-headless.ibm-storage-defender-dev.svc.cluster.local", mongoIp) .withStartupTimeout(Duration.ofMinutes(3)) .waitingFor(Wait.forHttp("/health").forStatusCode(200)); openLibertyContainer.start(); } }
The Error: Despite using .withExtraHost() to map the FQDNs to the container IP, and attempting to override the config via Environment Variables, the application crashes with a timeout caused by an UnknownHostException. It seems the driver is discovering the cluster topology and failing to reach the nodes.
Plaintext
com.mongodb.MongoTimeoutException: Timed out while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=REPLICA_SET, servers=[ {address=mongodb-0.mongodb-headless.ibm-storage-defender-dev.svc.cluster.local:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: mongodb-0.mongodb-headless.ibm-storage-defender-dev.svc.cluster.local}, caused by {java.net.UnknownHostException: mongodb-0.mongodb-headless.ibm-storage-defender-dev.svc.cluster.local}}, ... ]} at com.mongodb.internal.connection.BaseCluster.logAndThrowTimeoutException(BaseCluster.java:431) ... at com.ibm.storage.defender.service.mongodb.ClusterManagerImpl.createIndex(ClusterManagerImpl.java:94) What I have tried:
Environment Variables: Set MONGO_REPLICAS="0" and MONGO_HOSTS="testmongo:27017" to try and force a single-node configuration, but the logs show the client view is still expecting a REPLICA_SET with the K8s addresses.
DNS Mapping: Used .withExtraHost() to map the specific K8s FQDNs to the MongoDB container's internal Docker IP.
Network Aliases: Tried adding the FQDNs as .withNetworkAliases() on the MongoDB container side.
Environment:
Java 17 / Jakarta EE
Open Liberty (MicroProfile)
MongoDB Driver (Sync)
Testcontainers 1.19.x
Question: How can I force the Open Liberty application (or the underlying Mongo Driver) to ignore the K8s replica set topology and connect to the single testmongo container? Or why is withExtraHost failing to resolve the UnknownHostException in this Docker-in-Docker scenario?
(Note - Used ChatGPT to phrase the question )