You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #3066
MongoDBContainer's constructor did not set an explicit wait strategy, so it fell backto the default port-based wait strategy from
GenericContainer. This caused a racecondition when using init scripts (e.g. via
withCopyFileToContainertargeting/docker-entrypoint-initdb.d/), because MongoDB restarts internally after running theinit script, and the port-based wait strategy could report the container as "ready"
before the restart completed.
Changes
waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 1))to theMongoDBContainerconstructor, so the container waits for MongoDB's actual readinesslog message instead of relying solely on port availability.
shouldStartWithInitScript) that starts aMongoDBContainerwith aninit script copied to
/docker-entrypoint-initdb.d/, verifying the container startssuccessfully.
Testing
Ran the new test locally multiple times with
--rerunto confirm it passes consistently.