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

1 participant