diff --git a/src/Testcontainers/Clients/DockerContainerOperations.cs b/src/Testcontainers/Clients/DockerContainerOperations.cs index 73cf62a31..f695d3b23 100644 --- a/src/Testcontainers/Clients/DockerContainerOperations.cs +++ b/src/Testcontainers/Clients/DockerContainerOperations.cs @@ -32,26 +32,26 @@ 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/Clients/DockerImageOperations.cs b/src/Testcontainers/Clients/DockerImageOperations.cs index 5c6c2196b..041cd63e2 100644 --- a/src/Testcontainers/Clients/DockerImageOperations.cs +++ b/src/Testcontainers/Clients/DockerImageOperations.cs @@ -35,26 +35,26 @@ public async Task> GetAllAsync(FilterByProperty } public async Task ByIdAsync(string id, CancellationToken ct = default) + { + return await DockerClient.Images.InspectImageAsync(id, ct) + .ConfigureAwait(false); + } + + public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) { try { - return await DockerClient.Images.InspectImageAsync(id, ct) + _ = await ByIdAsync(id, ct) .ConfigureAwait(false); + + return true; } - catch (DockerApiException) + catch (DockerImageNotFoundException) { - 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 CreateAsync(IImage image, IDockerRegistryAuthenticationConfiguration dockerRegistryAuthConfig, CancellationToken ct = default) { var createParameters = new ImagesCreateParameters diff --git a/src/Testcontainers/Clients/DockerNetworkOperations.cs b/src/Testcontainers/Clients/DockerNetworkOperations.cs index d4a15af04..982ad2822 100644 --- a/src/Testcontainers/Clients/DockerNetworkOperations.cs +++ b/src/Testcontainers/Clients/DockerNetworkOperations.cs @@ -30,26 +30,26 @@ public async Task> GetAllAsync(FilterByProperty fil } public async Task ByIdAsync(string id, CancellationToken ct = default) + { + return await DockerClient.Networks.InspectNetworkAsync(id, ct) + .ConfigureAwait(false); + } + + public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) { try { - return await DockerClient.Networks.InspectNetworkAsync(id, ct) + _ = await ByIdAsync(id, ct) .ConfigureAwait(false); + + return true; } - catch (DockerApiException) + catch (DockerNetworkNotFoundException) { - 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 CreateAsync(INetworkConfiguration configuration, CancellationToken ct = default) { var createParameters = new NetworksCreateParameters diff --git a/src/Testcontainers/Clients/DockerVolumeOperations.cs b/src/Testcontainers/Clients/DockerVolumeOperations.cs index aebd74d71..c04fd42a4 100644 --- a/src/Testcontainers/Clients/DockerVolumeOperations.cs +++ b/src/Testcontainers/Clients/DockerVolumeOperations.cs @@ -34,26 +34,26 @@ public async Task> GetAllAsync(FilterByProperty filt } public async Task ByIdAsync(string id, CancellationToken ct = default) + { + return await DockerClient.Volumes.InspectAsync(id, ct) + .ConfigureAwait(false); + } + + public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) { try { - return await DockerClient.Volumes.InspectAsync(id, ct) + _ = await ByIdAsync(id, ct) .ConfigureAwait(false); + + return true; } catch (DockerApiException) { - 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 CreateAsync(IVolumeConfiguration configuration, CancellationToken ct = default) { var createParameters = new VolumesCreateParameters diff --git a/src/Testcontainers/Clients/TestcontainersClient.cs b/src/Testcontainers/Clients/TestcontainersClient.cs index 1d1090f66..576c51372 100644 --- a/src/Testcontainers/Clients/TestcontainersClient.cs +++ b/src/Testcontainers/Clients/TestcontainersClient.cs @@ -9,6 +9,7 @@ namespace DotNet.Testcontainers.Clients using System.Threading; using System.Threading.Tasks; using Docker.DotNet; + using Docker.DotNet.Models; using DotNet.Testcontainers.Builders; using DotNet.Testcontainers.Configurations; using DotNet.Testcontainers.Containers; @@ -286,6 +287,8 @@ public async Task ReadFileAsync(string id, string filePath, Cancellation /// public async Task RunAsync(IContainerConfiguration configuration, CancellationToken ct = default) { + ImageInspectResponse cachedImage; + if (TestcontainersSettings.ResourceReaperEnabled && ResourceReaper.DefaultSessionId.Equals(configuration.SessionId)) { var isWindowsEngineEnabled = await System.GetIsWindowsEngineEnabled(ct) @@ -295,8 +298,15 @@ public async Task RunAsync(IContainerConfiguration configuration, Cancel .ConfigureAwait(false); } - var cachedImage = await Image.ByIdAsync(configuration.Image.FullName, ct) - .ConfigureAwait(false); + try + { + cachedImage = await Image.ByIdAsync(configuration.Image.FullName, ct) + .ConfigureAwait(false); + } + catch (DockerImageNotFoundException) + { + cachedImage = null; + } if (configuration.ImagePullPolicy(cachedImage)) { @@ -325,8 +335,17 @@ await Task.WhenAll(configuration.ResourceMappings.Select(resourceMapping => Copy /// public async Task BuildAsync(IImageFromDockerfileConfiguration configuration, CancellationToken ct = default) { - var cachedImage = await Image.ByIdAsync(configuration.Image.FullName, ct) - .ConfigureAwait(false); + ImageInspectResponse cachedImage; + + try + { + cachedImage = await Image.ByIdAsync(configuration.Image.FullName, ct) + .ConfigureAwait(false); + } + catch (DockerImageNotFoundException) + { + cachedImage = null; + } if (configuration.ImageBuildPolicy(cachedImage)) { diff --git a/src/Testcontainers/Containers/DockerContainer.cs b/src/Testcontainers/Containers/DockerContainer.cs index 8cd148758..c51879071 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 (DockerContainerNotFoundException) + { + _container = new ContainerInspectResponse(); + } StoppedTime = DateTime.UtcNow; Stopped?.Invoke(this, EventArgs.Empty); @@ -517,7 +525,7 @@ await _client.StopAsync(_container.ID, ct) /// protected override bool Exists() { - return _container != null && ContainerHasBeenCreatedStates.HasFlag(State); + return ContainerHasBeenCreatedStates.HasFlag(State); } ///