| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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.
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:
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.
Sorry, something went wrong.
| 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)); |
| Back | FazBrowse Home | New Git URL |
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 with withReplicaSet(), the wait strategy is automatically adjusted to expect the log message twice, accounting for the extra restart. Without withReplicaSet(), 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();
```