aspire icon indicating copy to clipboard operation
aspire copied to clipboard

Docker Compose publisher not producing correct port mappings for PublishAsDockerfile

Open mitchdenny opened this issue 7 months ago • 3 comments

Image

mitchdenny avatar Apr 09 '25 12:04 mitchdenny

This works:

var frontend = builder.AddNpmApp("frontend", "../frontend", "dev")
                      .WithHttpEndpoint(env: "PORT")
                      .PublishAsDockerFile(
                        container => {
                            container.WithEndpoint("http", (ep) => {
                                ep.TargetPort = 3000;
                            });
                        }
                      );

mitchdenny avatar Apr 09 '25 13:04 mitchdenny

This also works:

var frontend = builder.AddNpmApp("frontend", "../frontend", "dev")
    .WithHttpEndpoint(targetPort: 3000, env: "PORT")
    .PublishAsDockerFile();

mitchdenny avatar Apr 09 '25 13:04 mitchdenny

We have the same issue with the K8s publisher currently too.

It all boils down to how env vars are being processed - the discussion I was having with David in this PR: https://github.com/dotnet/aspire/pull/8260#discussion_r2011027709 covers whats happening

We do have: context.EnvironmentVariables[env] = endpointReference.Property(EndpointProperty.TargetPort); but without the ability to look at the internal TargetPortEnvironmentVariable I don't see how we can control this and "know" that the env var you are processing is actually a port mapping, as they can be named anything?

If that "linking" of env var to the fact that it is an endpoint mapping was accessible / preserved, then when the port allocator is called, and when we are mapping, we'd be able to control it

prom3theu5 avatar Apr 11 '25 15:04 prom3theu5

the link is the EndpointReferenceExpression. This is handled by processing endpoints first, then storing a map of the allocated endpoints mapped to endpoint name. Then when you see endpointReference.Property(EndpointProperty.TargetPort) it's ultimately a reverse lookup to the endpoints that were mapped.

Taking a look at this now.

davidfowl avatar May 10 '25 03:05 davidfowl