Created
November 28, 2025 12:33
-
-
Save dibble-james/8c99e108d7e8b2e2fda72f37c6086087 to your computer and use it in GitHub Desktop.
Running Windows Containers in Aspire
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Aspire.Hosting.ApplicationModel; | |
| public class WindowsContainerResource : ExecutableResource | |
| { | |
| public WindowsContainerResource(string name) | |
| : base(name, "docker", Directory.GetCurrentDirectory()) | |
| { | |
| } | |
| public class Options | |
| { | |
| public List<(int HostPort, int ContainerPort, string Scheme)> PortMappings { get; } = []; | |
| public List<KeyValuePair<string, string>> Env { get; } = []; | |
| public Options WithPortMapping(int hostPort, int containerPort, string scheme) | |
| { | |
| PortMappings.Add((hostPort, containerPort, scheme)); | |
| return this; | |
| } | |
| public Options WithEnv(string key, string value) | |
| { | |
| Env.Add(new (key, value)); | |
| return this; | |
| } | |
| } | |
| } | |
| public static class WindowsContainerResourceExtensions | |
| { | |
| extension(IDistributedApplicationBuilder applicationBuilder) | |
| { | |
| public IResourceBuilder<WindowsContainerResource> AddWindowsContainer(string name, string image, Action<WindowsContainerResource.Options>? configure = null) | |
| { | |
| var options = new WindowsContainerResource.Options(); | |
| configure?.Invoke(options); | |
| var resource = new WindowsContainerResource(name); | |
| var builder = applicationBuilder.AddResource(resource) | |
| .WithArgs("run") | |
| .WithArgs("--rm") | |
| .WithArgs("--name", $"{name}-{Path.GetFileNameWithoutExtension(Path.GetRandomFileName())}"); | |
| foreach (var env in options.Env) | |
| { | |
| builder.WithArgs("-e", $"{env.Key}={env.Value}"); | |
| } | |
| foreach (var port in options.PortMappings) | |
| { | |
| builder = builder.WithArgs("-p", $"{port.HostPort}:{port.ContainerPort}"); | |
| } | |
| builder.WithArgs(image); | |
| return builder; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment