Skip to content

Add withInitScript support to MongoDBContainer (#3066) - #11954

Open
klouds27 wants to merge 1 commit into
testcontainers:mainfrom
klouds27:fix/3066-mongodb-init-scripts
Open

Add withInitScript support to MongoDBContainer (#3066)#11954
klouds27 wants to merge 1 commit into
testcontainers:mainfrom
klouds27:fix/3066-mongodb-init-scripts

Conversation

@klouds27

Copy link
Copy Markdown

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();
```

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>
@klouds27
klouds27 marked this pull request as ready for review July 29, 2026 20:06
@klouds27
klouds27 requested a review from a team as a code owner July 29, 2026 20:06
Copilot AI review requested due to automatic review settings July 29, 2026 20:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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));
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