diff --git a/src/Aspire.Hosting/Publishing/ResourceContainerImageManager.cs b/src/Aspire.Hosting/Publishing/ResourceContainerImageManager.cs index fee77df27b2..f3b999d0cbb 100644 --- a/src/Aspire.Hosting/Publishing/ResourceContainerImageManager.cs +++ b/src/Aspire.Hosting/Publishing/ResourceContainerImageManager.cs @@ -192,8 +192,8 @@ public async Task BuildImagesAsync(IEnumerable resources, Cancellatio if (!containerRuntimeHealthy) { - logger.LogError("Container runtime is not running or is unhealthy. Cannot build container images."); - throw new InvalidOperationException("Container runtime is not running or is unhealthy."); + logger.LogError("Container runtime '{ContainerRuntimeName}' is not running or is unhealthy. Cannot build container images.", ContainerRuntime.Name); + throw new InvalidOperationException($"Container runtime '{ContainerRuntime.Name}' is not running or is unhealthy."); } logger.LogDebug("{ContainerRuntimeName} is healthy", ContainerRuntime.Name); @@ -214,6 +214,22 @@ public async Task BuildImageAsync(IResource resource, CancellationToken cancella var options = await ResolveContainerBuildOptionsAsync(resource, cancellationToken).ConfigureAwait(false); + // Check if this resource needs a container runtime + if (await ResourcesRequireContainerRuntimeAsync([resource], cancellationToken).ConfigureAwait(false)) + { + logger.LogDebug("Checking {ContainerRuntimeName} health", ContainerRuntime.Name); + + var containerRuntimeHealthy = await ContainerRuntime.CheckIfRunningAsync(cancellationToken).ConfigureAwait(false); + + if (!containerRuntimeHealthy) + { + logger.LogError("Container runtime '{ContainerRuntimeName}' is not running or is unhealthy. Cannot build container image.", ContainerRuntime.Name); + throw new InvalidOperationException($"Container runtime '{ContainerRuntime.Name}' is not running or is unhealthy."); + } + + logger.LogDebug("{ContainerRuntimeName} is healthy", ContainerRuntime.Name); + } + if (resource is ProjectResource) { // If it is a project resource we need to build the container image diff --git a/tests/Aspire.Hosting.Tests/Publishing/ResourceContainerImageManagerTests.cs b/tests/Aspire.Hosting.Tests/Publishing/ResourceContainerImageManagerTests.cs index ae66957625d..48db09ccdca 100644 --- a/tests/Aspire.Hosting.Tests/Publishing/ResourceContainerImageManagerTests.cs +++ b/tests/Aspire.Hosting.Tests/Publishing/ResourceContainerImageManagerTests.cs @@ -616,12 +616,13 @@ public async Task BuildImageAsync_ThrowsInvalidOperationException_WhenDockerRunt var exception = await Assert.ThrowsAsync(() => imageBuilder.BuildImagesAsync([container.Resource], cts.Token)); - Assert.Equal("Container runtime is not running or is unhealthy.", exception.Message); + Assert.Contains("Container runtime", exception.Message); + Assert.Contains("is not running or is unhealthy", exception.Message); var collector = app.Services.GetFakeLogCollector(); var logs = collector.GetSnapshot(); - Assert.Contains(logs, log => log.Message.Contains("Container runtime is not running or is unhealthy. Cannot build container images.")); + Assert.Contains(logs, log => log.Message.Contains("is not running or is unhealthy. Cannot build container images.")); } [Fact]