diff --git a/src/Testcontainers.MongoDb/MongoDbBuilder.cs b/src/Testcontainers.MongoDb/MongoDbBuilder.cs index d7c0e752b..bbc017b83 100644 --- a/src/Testcontainers.MongoDb/MongoDbBuilder.cs +++ b/src/Testcontainers.MongoDb/MongoDbBuilder.cs @@ -198,6 +198,8 @@ private static async Task InitiateReplicaSetAsync(MongoDbContainer container, Mo return; } + var readiness = new WaitIndicateReadiness(configuration); + // This is a simple workaround to use the default options, which can be configured // with custom configurations as needed. var options = new WaitStrategy(); @@ -212,6 +214,14 @@ private static async Task InitiateReplicaSetAsync(MongoDbContainer container, Mo return 0L.Equals(execResult.ExitCode); }; + // The official MongoDB image starts mongod twice during the first-time + // initialization (bootstrap + final process). Waiting here prevents the + // replica set from being initiated while this handover is in progress, + // which could otherwise cause the container start to fail: + // https://github.com/testcontainers/testcontainers-dotnet/issues/1636. + await WaitStrategy.WaitUntilAsync(() => readiness.UntilAsync(container), options.Interval, options.Timeout, options.Retries, ct) + .ConfigureAwait(false); + await WaitStrategy.WaitUntilAsync(initiate, options.Interval, options.Timeout, options.Retries, ct) .ConfigureAwait(false); }