@@ -22,6 +22,7 @@ public class LocalStackManager : ILocalStackManager
2222 private IContainer ? _container ;
2323 private LocalStackConfiguration ? _configuration ;
2424 private bool _disposed ;
25+ private bool _isExternalInstance ;
2526 private readonly Dictionary < string , DateTime > _serviceReadyTimes = new ( ) ;
2627 private readonly object _lockObject = new ( ) ;
2728
@@ -31,7 +32,7 @@ public LocalStackManager(ILogger<LocalStackManager> logger)
3132 }
3233
3334 /// <inheritdoc />
34- public bool IsRunning => _container ? . State == TestcontainersStates . Running ;
35+ public bool IsRunning => _container ? . State == TestcontainersStates . Running || _isExternalInstance ;
3536
3637 /// <inheritdoc />
3738 public string Endpoint => _configuration ? . Endpoint ?? "http://localhost:4566" ;
@@ -54,7 +55,7 @@ public async Task StartAsync(LocalStackConfiguration config)
5455 if ( await IsExternalLocalStackAvailableAsync ( config . Endpoint ) )
5556 {
5657 _logger . LogInformation ( "Detected existing LocalStack instance at {Endpoint}, using it instead of starting new container" , config . Endpoint ) ;
57- // Don't start a new container, just use the existing one
58+ _isExternalInstance = true ;
5859 return ;
5960 }
6061
@@ -152,14 +153,22 @@ public async Task StartAsync(LocalStackConfiguration config)
152153 /// <inheritdoc />
153154 public async Task StopAsync ( )
154155 {
156+ if ( _isExternalInstance )
157+ {
158+ _logger . LogInformation ( "Using external LocalStack instance — skipping stop" ) ;
159+ _isExternalInstance = false ;
160+ _configuration = null ;
161+ return ;
162+ }
163+
155164 if ( _container == null )
156165 return ;
157-
166+
158167 _logger . LogInformation ( "Stopping LocalStack container" ) ;
159-
168+
160169 try
161170 {
162- if ( IsRunning )
171+ if ( _container . State == TestcontainersStates . Running )
163172 {
164173 await _container . StopAsync ( ) ;
165174 }
@@ -174,7 +183,7 @@ public async Task StopAsync()
174183 _container = null ;
175184 _configuration = null ;
176185 }
177-
186+
178187 _logger . LogInformation ( "LocalStack container stopped" ) ;
179188 }
180189
0 commit comments