Add support for host IP binding in Aspire.Hosting.Docker port mappings
- 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
Assessment
This issue has not been assessed yet.