diff --git a/src/Aspire.Hosting/ApplicationModel/ProjectResource.cs b/src/Aspire.Hosting/ApplicationModel/ProjectResource.cs index a52c3c2d56e..1e960005d69 100644 --- a/src/Aspire.Hosting/ApplicationModel/ProjectResource.cs +++ b/src/Aspire.Hosting/ApplicationModel/ProjectResource.cs @@ -163,10 +163,13 @@ private async Task BuildProjectImage(PipelineStepContext ctx) // Add COPY --from: statements for each source stage.AddContainerFiles(this, containerWorkingDir, logger); - // Get the directory service to create temp Dockerfile var projectDir = Path.GetDirectoryName(projectMetadata.ProjectPath)!; + + // Create a unique temporary Dockerfile path for this resource using the directory service. + // Passing a file name causes CreateTempFile to create the file in a new, empty subdirectory, + // which avoids Docker/buildx scanning the entire temporary directory. var directoryService = ctx.Services.GetRequiredService(); - var tempDockerfilePath = directoryService.TempDirectory.CreateTempFile().Path; + var tempDockerfilePath = directoryService.TempDirectory.CreateTempFile("Dockerfile").Path; var builtSuccessfully = false; try diff --git a/src/Aspire.Hosting/ContainerResourceBuilderExtensions.cs b/src/Aspire.Hosting/ContainerResourceBuilderExtensions.cs index 4d44532128b..b1e75a75be2 100644 --- a/src/Aspire.Hosting/ContainerResourceBuilderExtensions.cs +++ b/src/Aspire.Hosting/ContainerResourceBuilderExtensions.cs @@ -718,9 +718,11 @@ public static IResourceBuilder WithDockerfileFactory(this IResourceBuilder var fullyQualifiedContextPath = Path.GetFullPath(contextPath, builder.ApplicationBuilder.AppHostDirectory) .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - // Create a unique temporary Dockerfile path for this resource using the directory service + // Create a unique temporary Dockerfile path for this resource using the directory service. + // Passing a file name causes CreateTempFile to create the file in a new, empty subdirectory, + // which avoids Docker/buildx scanning the entire temporary directory. var directoryService = builder.ApplicationBuilder.FileSystemService; - var tempDockerfilePath = directoryService.TempDirectory.CreateTempFile().Path; + var tempDockerfilePath = directoryService.TempDirectory.CreateTempFile("Dockerfile").Path; var imageName = ImageNameGenerator.GenerateImageName(builder); var imageTag = ImageNameGenerator.GenerateImageTag(builder);