Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 917
MojoFilter Avatar asked Oct 27 '25 12:10

MojoFilter


2 Answers

I know this is a bit late, but I thought I would post this here as I spent a lot of time trying to figure this out.

I had a very similar issue with sqlserver. and wanting to connect with ssms i added this:

var sql = builder
    .AddSqlServer("sql", password)
    .WithLifetime(ContainerLifetime.Persistent)
    .WithDataBindMount(source: @"C:\SqlServer\Data")
    .WithEndpoint(port:6033, targetPort:1433, name:"ssms")
    .WithContainerName("sqlserver");

now I can connect with 127.0.0.1,6033 with my username and password.

like image 86
3xGuy Avatar answered Oct 29 '25 07:10

3xGuy


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 (not "localhost") as the host name in the connection strings used to connect to the instance of SQL Server.

like image 36
IDN-BWash Avatar answered Oct 29 '25 06:10

IDN-BWash