dotnet-aspire

Binding container port in aspire


I have a custom SQL Server Docker image which I would like to add to my Aspire project. The image exposes port 1433. How do I bind that to an endpoint assigned by Aspire? I plan on adding other containers in a similar way that have nothing to do with SQL Server once I get this working.

Configuring it like this runs the container but does not bind the port from the endpoint:

builder.AddContainer("custom-sql-container", "my.sql.image")
       .WithEndpoint(1433, 1433);

Surprise Update

I was looking at the container in docker desktop, where it was not showing a bound port at all:

container showing in docker desktop with no port

But docker ps gave me this spicy little nugget: result of docker ps on the command line showing a bound port

So it is binding the port, just not to what I expected it to be. Does this mean that aspire just hasn't set up a proxy for that port or do I just completely misunderstand how this is supposed to be configured?


Solution

  • SQL Server will, by default, only listen for connections on the loopback interface [127.0.0.1] or [0.0.0.0]. That means even ports that are EXPOSED to an endpoint that has been assigned by Aspire, any external connections trying to reach your SQL Server Instance will not be able to find it.

    Resolution: You could use 127.0.0.1 as the host name in the connection strings used to connect to the instance of SQL Server.