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