Skip to content

Conversation

@taole33
Copy link

@taole33 taole33 commented Dec 6, 2025

Fixes #3066

Description

This PR adds support for initialization scripts in MongoDBContainer.

When an initialization script is placed in /docker-entrypoint-initdb.d/, the official MongoDB image restarts the container process after executing the script. This behavior previously caused WaitStrategy timeouts because the container would stop and start again.

Changes

  • Added withInitScript(String scriptPath) method to MongoDBContainer.
  • The method copies the script to /docker-entrypoint-initdb.d/init.js.
  • It automatically configures the WaitStrategy to expect two "waiting for connections" log messages, ensuring the container is fully ready after the restart.

Verification

  • Added MongoDBInitScriptTest to verify that the container starts successfully with an init script.

Running initialization scripts in MongoDB causes the container to restart, which previously required manual WaitStrategy adjustment.

This commit adds 'withInitScript(String)' to automatically handle the script copy and adjust the WaitStrategy to expect two startup log messages.

Fixes testcontainers#3066
@taole33 taole33 requested a review from a team as a code owner December 6, 2025 01:22
@eddumelendez eddumelendez changed the title feat(module/mongodb): Add support for initialization scripts Add support for initialization scripts Dec 9, 2025
* @param scriptPath the path to the init script file on the classpath
* @return this container instance
*/
public MongoDBContainer withInitScript(String scriptPath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

org.testcontainers.containers.MongoDBContainer is deprecated. can you moved to org.testcontainers.mongodb.MongoDBContainer?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved to org.testcontainers.mongodb.MongoDBContainer.
Also, For the tests, instead of creating a new file named MongoDBInitScriptTest.java, I added the test cases directly to MongoDBContainerTest.java.

@taole33 taole33 force-pushed the feature/issue-3066-mongodb-init-script branch 2 times, most recently from 6665ad7 to 67088da Compare December 9, 2025 23:28
@taole33 taole33 force-pushed the feature/issue-3066-mongodb-init-script branch from 67088da to e81a2ac Compare December 9, 2025 23:32
@eddumelendez
Copy link
Member

does this work with ReplicaSet and Sharded mode? New MongoDBContainer implementation has withReplicaSet() and withSharding() methods. It would be nice we can make sure this work with all implementations

@taole33
Copy link
Author

taole33 commented Dec 11, 2025

I updated withInitScript to support ReplicaSet and Sharding modes.

In these modes, the default entrypoint mechanism conflicts with the cluster initialization scripts or is ignored. To fix this, I modified the logic to isolate the init script from the default directory when running in Cluster mode, and instead execute it manually after the container has started.

Reason

In Cluster modes (ReplicaSet/Sharding), relying on the default /docker-entrypoint-initdb.d/ mechanism causes issues:

  • ReplicaSet: The default entrypoint triggers a restart, causing a race condition with rs.initiate().
  • Sharding: The entrypoint is overridden, so the script is ignored.

Changes

  • Added logic to detect Cluster mode.
  • If Cluster mode is enabled, the script is mounted to a temporary location (isolating it from the default entrypoint) and executed manually after the cluster is fully initialized.
  • This ensures data consistency and avoids startup conflicts.

Fixes #3066

Comment on lines +61 to +63
.withEnv("LANG", "C.UTF-8")
.withEnv("LC_ALL", "C.UTF-8")
.withStartupTimeout(Duration.ofSeconds(30))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this required?

try (
MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10")
.withInitScript("init.js")
.withStartupTimeout(Duration.ofSeconds(30))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this required?

.isEqualTo("{ } [ ] : ,");

assertThat(doc.getString("description"))
.as("Japanese text should be preserved correctly")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove as


assertThat(doc.getString("description"))
.as("Japanese text should be preserved correctly")
.isEqualTo("特殊記号を含むコレクションへの挿入テスト");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use plain english, please?

Comment on lines +126 to +132
@Test
void shouldExecuteInitScriptWithShardingConfiguredFirst() {
try (MongoDBContainer mongo = new MongoDBContainer("mongo:7.0.0").withSharding().withInitScript("init.js")) {
mongo.start();
assertInitScriptExecuted(mongo);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove it. There is no difference with the previous test

Comment on lines +110 to +116
@Test
void shouldExecuteInitScriptWithReplicaSetConfiguredFirst() {
try (MongoDBContainer mongo = new MongoDBContainer("mongo:7.0.0").withReplicaSet().withInitScript("init.js")) {
mongo.start();
assertInitScriptExecuted(mongo);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove it. There is no difference with the previous test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support init scripts for MongoDBContainer without manually customizing the WaitStrategy

2 participants