microsoft / microsoft/aspire

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

Open
#13,460 0 comments 1 reaction 0 assignees View on GitHub
area-app-model
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] 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 WithHostBinding(this IResourceBuilder builder, string hostAddress) where : IComputeResource, IResourceWithEndpoints;

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

### Additional context

Workaround:

```
public static IResourceBuilder WithHostBinding(this IResourceBuilder 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}";
}
});
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.