dotnet / sdk-container-builds

Libraries and build tooling to create container images from .NET projects using MSBuild
https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container
MIT License
176 stars 30 forks source link

Create a ContainerPort item for the ASPNETCORE_URLs environment variables if set #478

Closed baronfel closed 11 months ago

baronfel commented 11 months ago

ASP.NET has a few environment variables that control which ports/urls are used for hosting the application

We should check for these in the environment block of an application, and attempt to define ExposedPort metadata for any ports found there. ASPNETCORE_HTTP_PORTS and ASPNETCORE_HTTPS_PORTS have a simple syntax of semicolon-delimited port numbers, but ASPNETCORE_URLS has a format of semicolon-delimited {scheme}://{address}:{port} items, where address is one of:

Creating these ports allows other tooling like the VSCode Docker tooling to automatically detect and expose ports when users run our containers.

danegsta commented 11 months ago

@baronfel as a heads up, this is the parser from the aspnetcore project for parsing the entries in ASPNETCORE_URLS: https://github.com/dotnet/aspnetcore/blob/main/src/Http/Http/src/BindingAddress.cs

baronfel commented 11 months ago

@danegsta one concern I have with using their parser is that it changes the runtime requirements of our MSBuild Tasks. Before they worked with just the basic .NET runtime installed. Using the AspNet parser directly for this would start requiring the Microsoft.AspNetcore.App FrameworkReference, which might have implications. I'll figure it out with the team though.

baronfel commented 11 months ago

Yeah, this is going to be A Thing :tm:

danegsta commented 11 months ago

@baronfel the main complication comes from the fact that the URLs they use aren't always real valid URLs; they use special characters to represent binding to any address (usually noted with 0.0.0.0 elsewhere), so http://+:80;https://+:443 is a totally valid ASPNETCORE_URLS value. You can probably get away with simple split on ';' plus a simple regex to match <protocol>://<host like value>:<port> and call it a day.