Skip to content

Fix MongoDBContainer default wait strategy to support init scripts (#3066) - #11965

Open
devsuyash-Hq wants to merge 1 commit into
testcontainers:mainfrom
devsuyash-Hq:fix-3066-mongodb-init-script-wait
Open

Fix MongoDBContainer default wait strategy to support init scripts (#3066)#11965
devsuyash-Hq wants to merge 1 commit into
testcontainers:mainfrom
devsuyash-Hq:fix-3066-mongodb-init-script-wait

Conversation

@devsuyash-Hq

Copy link
Copy Markdown

What does this PR do?

Fixes #3066

MongoDBContainer's constructor did not set an explicit wait strategy, so it fell back
to the default port-based wait strategy from GenericContainer. This caused a race
condition when using init scripts (e.g. via withCopyFileToContainer targeting
/docker-entrypoint-initdb.d/), because MongoDB restarts internally after running the
init script, and the port-based wait strategy could report the container as "ready"
before the restart completed.

Changes

  • Added waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 1)) to the
    MongoDBContainer constructor, so the container waits for MongoDB's actual readiness
    log message instead of relying solely on port availability.
  • Added a test (shouldStartWithInitScript) that starts a MongoDBContainer with an
    init script copied to /docker-entrypoint-initdb.d/, verifying the container starts
    successfully.

Testing

Ran the new test locally multiple times with --rerun to confirm it passes consistently.

@devsuyash-Hq
devsuyash-Hq requested a review from a team as a code owner August 11, 2026 19:56
@devsuyash-Hq

Copy link
Copy Markdown
Author

This PR fixes #3066.

The root cause: MongoDBContainer's constructor never set an explicit wait
strategy, so it relied on the default port-based check from GenericContainer.
When an init script is copied to /docker-entrypoint-initdb.d/, MongoDB restarts
internally after running it — and the port-based check could report the container
"ready" before that restart finished, causing the flaky behavior described in the
issue.

Fix: added waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 1))
to the constructor, so it waits on MongoDB's actual readiness log instead of
just the port.

Also added a test (shouldStartWithInitScript) that starts a container with an
init script, verified passing consistently across multiple runs.

Happy to adjust if maintainers prefer a different approach (e.g. times: 2
conditionally when an init script is present).

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