Add withInitScript support to MongoDBContainer (#3066) - #11954
Open
klouds27 wants to merge 1 commit into
Open
Conversation
When an init script is placed in /docker-entrypoint-initdb.d/, MongoDB shuts down and restarts after executing it. The existing wait strategy only waited for one "waiting for connections" log line, so startup appeared to complete before the restart, causing intermittent failures. withInitScript(MountableFile) copies the script into the container at startup. When used together with withReplicaSet(), the wait strategy is automatically bumped to expect the log message twice, covering the extra restart MongoDB performs after running init scripts. Signed-off-by: klouds27 <adalwolf@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR adds first-class init-script support to MongoDBContainer so users can copy JavaScript files into /docker-entrypoint-initdb.d/ and have startup reliably wait for MongoDB’s post-init restart when running with a replica set (addressing #3066).
Changes:
- Added
withInitScript(MountableFile)API to register one or more init scripts and copy them into the container. - Adjusted the replica set startup wait strategy to handle the extra restart triggered by init scripts.
- Added integration tests (and a test init script) covering init scripts with and without replica sets.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| modules/mongodb/src/main/java/org/testcontainers/mongodb/MongoDBContainer.java | Adds init-script registration/copying and adjusts wait strategy for replica-set startup with init scripts. |
| modules/mongodb/src/test/java/org/testcontainers/mongodb/MongoDBContainerTest.java | Adds tests asserting init scripts execute correctly in replica set and standalone modes. |
| modules/mongodb/src/test/resources/init-db.js | Adds a simple init script used by the new tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+68
to
+72
| for (int i = 0; i < initScripts.size(); i++) { | ||
| copyFileToContainer(initScripts.get(i), "/docker-entrypoint-initdb.d/init-" + i + ".js"); | ||
| } | ||
| if (this.rsEnabled && !initScripts.isEmpty()) { | ||
| waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3066
When a script is placed in
/docker-entrypoint-initdb.d/, MongoDB shuts down and restarts after executing it. The existing wait strategy only expected"waiting for connections"once, so startup appeared to complete before the restart finished, causing containers with init scripts to fail.withInitScript(MountableFile)copies the script into the container at startup. When combined withwithReplicaSet(), the wait strategy is automatically adjusted to expect the log message twice, accounting for the extra restart. WithoutwithReplicaSet(), no wait strategy change is needed since MongoDB does not restart in standalone mode.Multiple scripts can be chained and are executed in the order they were added.
```java
new MongoDBContainer("mongo:6.0")
.withReplicaSet()
.withInitScript(MountableFile.forClasspathResource("init-db.js"))
.start();
```