testcontainers/testcontainers-java · error · ReplicaSetInitializationException
A single node replica set was not initialized in a set…
Error message
A single node replica set was not initialized in a set timeout: %d attempts
What it means
After issuing rs.initiate, MongoDBContainer repeatedly polls (up to AWAIT_INIT_REPLICA_SET_ATTEMPTS) for a single-node replica set master. checkMongoNodeExitCodeAfterWaiting throws ReplicaSetInitializationException when the poll command keeps exiting non-zero and the master never appears within the attempt budget.
Solutions
- Retry the test run — transient slowness on loaded CI hosts is common
- Use a supported, recent official mongo image
- Allocate more CPU/memory to Docker (or the CI runner) to speed up replica set election
- Enable container log following to diagnose why the master never becomes primary
Example fix
// before
new MongoDBContainer("mongo:3.6") // old image, flaky election on busy CI
// after
new MongoDBContainer(DockerImageName.parse("mongo:6.0"));
// and ensure the runner has enough CPU; rerun if transient Defensive patterns
Strategy: retry
Try / catch
try {
mongo.start();
} catch (ReplicaSetInitializationException e) {
if (e.getMessage().contains("was not initialized")) {
mongo.stop();
mongo.start(); // retry once; election delays are often transient
} else {
throw e;
}
} Prevention
- Provision enough CPU/memory for Docker on CI runners
- Use a recent, supported mongo image version
- Rerun flaky init failures before treating them as code bugs
When it happens
Trigger: The wait-for-master eval command fails on every attempt: slow container host making all attempts time out, mongod crashing after start, or an image whose mongo shell rejects the wait command.
Common situations: Heavily loaded CI runners where replica set election is slow; restricted cgroup/CPU environments; incompatible mongo image versions; Docker storage/network issues inside the container.
Understand the failure class
Background: Request timed out: what client-side request timeouts mean across libraries (Request timed out, TIMED_OUT, APITimeoutError) — this error's family across 39 libraries.
- Timeouts: ETIMEDOUT, deadlines, and hung requests — what actually expires when a request times out.
Related errors
- A single node replica set was not initialized in a set…
- An error occurred
- An error occurred
- Changing startup timeout is not supported with mode
- MongoDBContainer should be started first
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/d55ba0e6e4294542.
Report an issue: GitHub.
Appendix: source
Thrown at modules/mongodb/src/main/java/org/testcontainers/containers/MongoDBContainer.java:169
"(%s) " +
"{ " +
"if (attempt > %d) {quit(1);} " +
"print('%s ' + attempt); sleep(100); attempt++; " +
" }",
"db.runCommand( { isMaster: 1 } ).ismaster==false",
AWAIT_INIT_REPLICA_SET_ATTEMPTS,
"An attempt to await for a single node replica set initialization:"
);
}
private void checkMongoNodeExitCodeAfterWaiting(final Container.ExecResult execResultWaitForMaster) {
if (execResultWaitForMaster.getExitCode() != CONTAINER_EXIT_CODE_OK) {
final String errorMessage = String.format(
"A single node replica set was not initialized in a set timeout: %d attempts",
AWAIT_INIT_REPLICA_SET_ATTEMPTS
);
log.error(errorMessage);
throw new ReplicaSetInitializationException(errorMessage);
}
}
@SneakyThrows(value = { IOException.class, InterruptedException.class })
private void initReplicaSet(boolean reused) {
if (reused && isReplicationSetAlreadyInitialized()) {
log.debug("Replica set already initialized.");
} else {
log.debug("Initializing a single node node replica set...");
final ExecResult execResultInitRs = execInContainer(buildMongoEvalCommand("rs.initiate();"));
log.debug(execResultInitRs.getStdout());
checkMongoNodeExitCode(execResultInitRs);
log.debug(
"Awaiting for a single node replica set initialization up to {} attempts",
AWAIT_INIT_REPLICA_SET_ATTEMPTS
);
final ExecResult execResultWaitForMaster = execInContainer(buildMongoEvalCommand(buildMongoWaitCommand()));View on GitHub (pinned to 8e549514e3)