Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Aspire.Hosting.Docker/DockerComposeEnvironmentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Docker;
using Aspire.Hosting.Docker.Resources;
using Aspire.Hosting.Lifecycle;

namespace Aspire.Hosting;
Expand Down Expand Up @@ -38,4 +39,35 @@ public static IResourceBuilder<DockerComposeEnvironmentResource> AddDockerCompos
}
return builder.AddResource(resource);
}

/// <summary>
/// Allows setting the properties of a Docker Compose environment resource.
/// </summary>
/// <param name="builder">The Docker Compose environment resource builder.</param>
/// <param name="configure">A method that can be used for customizing the <see cref="DockerComposeEnvironmentResource"/>.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<DockerComposeEnvironmentResource> WithProperties(this IResourceBuilder<DockerComposeEnvironmentResource> builder, Action<DockerComposeEnvironmentResource> configure)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(configure);

configure(builder.Resource);

return builder;
}

/// <summary>
/// Configures the Docker Compose file for the environment resource.
/// </summary>
/// <param name="builder"> The Docker compose environment resource builder.</param>
/// <param name="configure">A method that can be used for customizing the <see cref="ComposeFile"/>.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<DockerComposeEnvironmentResource> ConfigureComposeFile(this IResourceBuilder<DockerComposeEnvironmentResource> builder, Action<ComposeFile> configure)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(configure);

builder.Resource.ConfigureComposeFile += configure;
return builder;
}
}
13 changes: 13 additions & 0 deletions src/Aspire.Hosting.Docker/DockerComposeEnvironmentResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma warning disable ASPIRECOMPUTE001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Docker.Resources;

namespace Aspire.Hosting.Docker;

Expand All @@ -16,6 +17,18 @@ namespace Aspire.Hosting.Docker;
/// <param name="name">The name of the Docker Compose environment.</param>
public class DockerComposeEnvironmentResource(string name) : Resource(name), IComputeEnvironmentResource
{
/// <summary>
/// The container registry to use.
/// </summary>
public string? DefaultContainerRegistry { get; set; }

/// <summary>
/// The name of an existing network to be used.
/// </summary>
public string? DefaultNetworkName { get; set; }

internal Action<ComposeFile>? ConfigureComposeFile { get; set; }

/// <summary>
/// Gets the collection of environment variables captured from the Docker Compose environment.
/// These will be populated into a top-level .env file adjacent to the Docker Compose file.
Expand Down
10 changes: 0 additions & 10 deletions src/Aspire.Hosting.Docker/DockerComposePublisherOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ namespace Aspire.Hosting.Docker;
/// </summary>
public sealed class DockerComposePublisherOptions : PublishingOptions
{
/// <summary>
/// The container registry to use.
/// </summary>
public string? DefaultContainerRegistry { get; set; }

/// <summary>
/// The name of an existing network to be used.
/// </summary>
public string? ExistingNetworkName { get; set; }

/// <summary>
/// Indicates whether to build container images during the publishing process.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private async Task WriteDockerComposeOutputAsync(DistributedApplicationModel mod

var defaultNetwork = new Network
{
Name = PublisherOptions.ExistingNetworkName ?? "aspire",
Name = environment.DefaultNetworkName ?? "aspire",
Driver = "bridge",
};

Expand Down Expand Up @@ -110,6 +110,9 @@ private async Task WriteDockerComposeOutputAsync(DistributedApplicationModel mod
}
}

// Call the environment's ConfigureComposeFile method to allow for custom modifications
environment.ConfigureComposeFile?.Invoke(composeFile);

var composeOutput = composeFile.ToYaml();
var outputFile = Path.Combine(PublisherOptions.OutputPath!, "docker-compose.yaml");
Directory.CreateDirectory(PublisherOptions.OutputPath!);
Expand Down
19 changes: 15 additions & 4 deletions tests/Aspire.Hosting.Docker.Tests/DockerComposePublisherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Xunit;
using Aspire.Hosting.Docker.Resources.ComposeNodes;

namespace Aspire.Hosting.Docker.Tests;

Expand Down Expand Up @@ -285,7 +286,14 @@ public async Task DockerComposeAppliesServiceCustomizations()
var options = new OptionsMonitor(new DockerComposePublisherOptions { OutputPath = tempDir.Path });
var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);

builder.AddDockerComposeEnvironment("docker-compose");
builder.AddDockerComposeEnvironment("docker-compose")
.WithProperties(e => e.DefaultNetworkName = "default-network")
.ConfigureComposeFile(file =>
{
file.AddNetwork(new Network { Name = "custom-network", Driver = "host" });

file.Name = "my application";
});

// Add a container to the application
var container = builder.AddContainer("service", "nginx")
Expand Down Expand Up @@ -329,21 +337,24 @@ public async Task DockerComposeAppliesServiceCustomizations()

Assert.Equal(
"""
name: "my application"
services:
service:
image: "nginx:latest"
environment:
ORIGINAL_ENV: "value"
CUSTOM_ENV: "custom-value"
networks:
- "aspire"
- "custom-network"
- "default-network"
- "custom-network"
restart: "always"
labels:
custom-label: "test-value"
networks:
aspire:
default-network:
driver: "bridge"
custom-network:
driver: "host"

""",
content, ignoreAllWhiteSpace: true, ignoreLineEndingDifferences: true);
Expand Down
Loading