{"record":{"id":"b7b0d1393b2d448f","repo":"testcontainers/testcontainers-java","slug":"an-error-occurred-s","errorCode":null,"errorMessage":"An error occurred: %s","messagePattern":"An error occurred: (.+?)","errorType":"exception","errorClass":"ReplicaSetInitializationException","httpStatus":null,"severity":"error","filePath":"modules/mongodb/src/main/java/org/testcontainers/containers/MongoDBContainer.java","lineNumber":143,"sourceCode":"        if (!isRunning()) {\n            throw new IllegalStateException(\"MongoDBContainer should be started first\");\n        }\n        return getConnectionString() + \"/\" + databaseName;\n    }\n\n    private String[] buildMongoEvalCommand(final String command) {\n        return new String[] {\n            \"sh\",\n            \"-c\",\n            \"mongosh mongo --eval \\\"\" + command + \"\\\"  || mongo --eval \\\"\" + command + \"\\\"\",\n        };\n    }\n\n    private void checkMongoNodeExitCode(final Container.ExecResult execResult) {\n        if (execResult.getExitCode() != CONTAINER_EXIT_CODE_OK) {\n            final String errorMessage = String.format(\"An error occurred: %s\", execResult.getStdout());\n            log.error(errorMessage);\n            throw new ReplicaSetInitializationException(errorMessage);\n        }\n    }\n\n    private String buildMongoWaitCommand() {\n        return String.format(\n            \"var attempt = 0; \" +\n            \"while\" +\n            \"(%s) \" +\n            \"{ \" +\n            \"if (attempt > %d) {quit(1);} \" +\n            \"print('%s ' + attempt); sleep(100);  attempt++; \" +\n            \" }\",\n            \"db.runCommand( { isMaster: 1 } ).ismaster==false\",\n            AWAIT_INIT_REPLICA_SET_ATTEMPTS,\n            \"An attempt to await for a single node replica set initialization:\"\n        );\n    }\n","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/mongodb/src/main/java/org/testcontainers/containers/MongoDBContainer.java#L125-L161","documentation":"During replica set initialization, MongoDBContainer runs mongo CLI commands inside the container. checkMongoNodeExitCode inspects the ExecResult; a non-zero exit code means the mongo node command failed, so the stdout is formatted into this message and thrown as ReplicaSetInitializationException.","triggerScenarios":"initReplicaSet() executes an rs.initiate/eval command that exits non-zero (mongo binary missing in the image, authentication enabled on the image, unsupported image variant, corrupted container state).","commonSituations":"Using a custom or slim MongoDB image without the mongod/mongo shell; connecting to an image with auth pre-enabled; version mismatch where the eval command syntax is unsupported.","solutions":["Check the stdout embedded in the message for the underlying mongo error","Use an official, supported mongo image (DockerImageName.parse(\"mongo:x.y\"))","Ensure the image does not enable auth or conflicting replica settings at startup","Increase logging (follow container output) to capture the failing command"],"exampleFix":"// before\nMongoDBContainer mongo = new MongoDBContainer(\"mycompany/mongo-custom:latest\"); // shell missing -> eval fails\n// after\nMongoDBContainer mongo = new MongoDBContainer(DockerImageName.parse(\"mongo:6.0\"));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    mongo.start();\n} catch (ReplicaSetInitializationException e) {\n    // stdout of the failed mongo command is embedded in the message\n    throw new IllegalStateException(\"Replica set init command failed: \" + e.getMessage(), e);\n}","preventionTips":["Use official mongo images Testcontainers is compatible with","Do not enable auth or custom replica config on the image","Enable container log following during debugging"],"tags":["mongodb","testcontainers","replica-set","docker"],"backgroundTag":"database-query-failed","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}