Skip to content

Add support for host IP binding in Aspire.Hosting.Docker port mappings #13460

@alienwareone

Description

@alienwareone

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Currently there is no way to specify the host IP for ports in Aspire.Hosting.Docker. The .WithEndpoint() API only generates mappings such as "5001:5001" in docker-compose.yaml. There is no mechanism to bind a port to a specific IP (for example, "127.0.0.1:5001:5001"). This limitation prevents developers from restricting services to localhost or a particular interface.

https://docs.docker.com/reference/cli/docker/container/run/#publish
https://learn.microsoft.com/en-us/dotnet/aspire/get-started/docker-compose-to-apphost-reference#port-mappings

Describe the solution you'd like

public static IResourceBuilder<T> WithHostBinding<T>(this IResourceBuilder<T> builder, string hostAddress) where  : IComputeResource, IResourceWithEndpoints;

public static IResourceBuilder<T> WithLoopbackBinding<T>(this IResourceBuilder<T> builder) where  : IComputeResource, IResourceWithEndpoints
    => WithHostBinding(builder, "127.0.0.1");

Additional context

Workaround:

public static IResourceBuilder<T> WithHostBinding<T>(this IResourceBuilder<T> builder, string hostAddress) where T : IComputeResource, IResourceWithEndpoints
{
    return builder
        .WithExternalHttpEndpoints()
        .PublishAsDockerComposeService((resource, service) =>
        {
            for (var i = 0; i < service.Ports.Count; i++)
            {
                var port = service.Ports[i];

                if (!port.Contains(':'))
                {
                    port = $"{port}:{port}";
                }

                service.Ports[i] = $"{hostAddress}:{port}";
            }
        });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions