aspire
aspire copied to clipboard
Docker Compose publisher not producing correct port mappings for PublishAsDockerfile
This works:
var frontend = builder.AddNpmApp("frontend", "../frontend", "dev")
.WithHttpEndpoint(env: "PORT")
.PublishAsDockerFile(
container => {
container.WithEndpoint("http", (ep) => {
ep.TargetPort = 3000;
});
}
);
This also works:
var frontend = builder.AddNpmApp("frontend", "../frontend", "dev")
.WithHttpEndpoint(targetPort: 3000, env: "PORT")
.PublishAsDockerFile();
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
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.