@@ -5,6 +5,7 @@ namespace DotNet.Testcontainers.Containers
55 using System . Globalization ;
66 using System . IO ;
77 using System . Linq ;
8+ using System . Text ;
89 using System . Threading ;
910 using System . Threading . Tasks ;
1011 using Docker . DotNet ;
@@ -635,6 +636,9 @@ private async Task<bool> CheckReadinessAsync(WaitStrategy waitStrategy, Cancella
635636 _container = await _client . Container . ByIdAsync ( _container . ID , ct )
636637 . ConfigureAwait ( false ) ;
637638
639+ await ThrowIfExitedAsync ( ct )
640+ . ConfigureAwait ( false ) ;
641+
638642 return await waitStrategy . UntilAsync ( this , ct )
639643 . ConfigureAwait ( false ) ;
640644 }
@@ -660,6 +664,39 @@ await WaitStrategy.WaitUntilAsync(() => CheckReadinessAsync(waitStrategy, ct), w
660664 return true ;
661665 }
662666
667+ private async Task ThrowIfExitedAsync ( CancellationToken ct = default )
668+ {
669+ if ( State == TestcontainersStates . Exited )
670+ {
671+ var message = new StringBuilder ( $ "The { Image . FullName } container has exited.") ;
672+ try
673+ {
674+ var ( stdout , stderr ) = await GetLogsAsync ( ct : ct )
675+ . ConfigureAwait ( false ) ;
676+
677+ stdout = stdout . Trim ( ) ;
678+ stderr = stderr . Trim ( ) ;
679+
680+ message . AppendLine ( ) ;
681+
682+ if ( stderr . Length > 0 )
683+ {
684+ message . AppendLine ( ) . AppendLine ( $ "=== stderr of { Name . TrimStart ( '/' ) } ({ Id } ) ===") . AppendLine ( stderr ) ;
685+ }
686+
687+ if ( stdout . Length > 0 )
688+ {
689+ message . AppendLine ( ) . AppendLine ( $ "=== stdout of { Name . TrimStart ( '/' ) } ({ Id } ) ===") . AppendLine ( stdout ) ;
690+ }
691+ }
692+ catch
693+ {
694+ message . AppendLine ( " Please look at the container logs." ) ;
695+ }
696+ throw new ContainerException ( message . ToString ( ) ) ;
697+ }
698+ }
699+
663700 private sealed class WaitUntilPortBindingsMapped : WaitStrategy
664701 {
665702 private readonly DockerContainer _parent ;
0 commit comments