Self registration

Hello, I have a question that has been bothering me for a while, but for which I have not yet found a solution.
I run Consul in a docker container (http://localhost:8500).
I also have a WebApi written in C#. I want this api to register itself on Consul. This works fine (as long as this api is not running in the docker container).

var registration = new AgentServiceRegistration();
            hostApplicationLifetime.ApplicationStarted.Register(() =>
                    var addresses = server.Features.Get<IServerAddressesFeature>();
                    var address = addresses.Addresses.First();
                    var uri = new Uri(address);

                    registration = new AgentServiceRegistration()
                        ID = $"{serviceName}-{uri.Port}",
                        Name = $"{serviceName}",
                        Address = $"{uri.Host}",
                        Port = uri.Port

                    logger.LogInformation("Registering with Consul");
            lifetime.ApplicationStopping.Register(() =>
                logger.LogInformation("Unregistering from Consul");

Here the application “knows” the port it is running on.
But when I put this application into a Docker container, it doesn’t work anymore. Because now the application would have to log on to Consul with the port of the container, as I understand it.
How can this be done? The application should “know” this port and use it for registration.
Thanks for the help in advance!