diff --git a/src/Testcontainers/Clients/DockerContainerOperations.cs b/src/Testcontainers/Clients/DockerContainerOperations.cs index 73cf62a31..a08a08593 100644 --- a/src/Testcontainers/Clients/DockerContainerOperations.cs +++ b/src/Testcontainers/Clients/DockerContainerOperations.cs @@ -32,26 +32,25 @@ public async Task> GetAllAsync(FilterByProper } public async Task ByIdAsync(string id, CancellationToken ct = default) + { + return await DockerClient.Containers.InspectContainerAsync(id, ct) + .ConfigureAwait(false); + } + + public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) { try { - return await DockerClient.Containers.InspectContainerAsync(id, ct) + await ByIdAsync(id, ct) .ConfigureAwait(false); + return true; } - catch (DockerApiException) + catch (DockerContainerNotFoundException) { - return null; + return false; } } - public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) - { - var response = await ByIdAsync(id, ct) - .ConfigureAwait(false); - - return response != null; - } - public async Task GetExitCodeAsync(string id, CancellationToken ct = default) { var response = await DockerClient.Containers.WaitContainerAsync(id, ct) diff --git a/src/Testcontainers/Containers/DockerContainer.cs b/src/Testcontainers/Containers/DockerContainer.cs index 8cd148758..b0e1143f0 100644 --- a/src/Testcontainers/Containers/DockerContainer.cs +++ b/src/Testcontainers/Containers/DockerContainer.cs @@ -7,6 +7,7 @@ namespace DotNet.Testcontainers.Containers using System.Linq; using System.Threading; using System.Threading.Tasks; + using Docker.DotNet; using Docker.DotNet.Models; using DotNet.Testcontainers.Clients; using DotNet.Testcontainers.Configurations; @@ -507,8 +508,15 @@ protected virtual async Task UnsafeStopAsync(CancellationToken ct = default) await _client.StopAsync(_container.ID, ct) .ConfigureAwait(false); - _container = await _client.Container.ByIdAsync(_container.ID, ct) - .ConfigureAwait(false); + try + { + _container = await _client.Container.ByIdAsync(_container.ID, ct) + .ConfigureAwait(false); + } + catch (DockerApiException) + { + _container = null; + } StoppedTime = DateTime.UtcNow; Stopped?.Invoke(this, EventArgs.Empty);