Docker external port

Wojciech Kusch 1 Reputation point
2022-03-31T20:07:43.48+00:00

Hi, I have an asp.net core app (service) inside a docker container. I have a docker-compose.yaml that starts this container

Port: 9010 --> 8070 (inside docker-compose)

I use Consul for container registration and try to self-register my service. For that - as I understand- I need the external port (9010).
How I can get this with C#?
Thanks for help!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,504 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,827 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 63,916 Reputation points
    2022-03-31T21:35:29.49+00:00

    its not clear what C# has to do with the question. you configured your docker container to listen on port 9010 and froward to a program in the container listening on port 8070.

    normally port 9010 would be blocked the host firewall rules, so you need to open the fire to access the docker container external from the host.


  2. Wojciech Kusch 1 Reputation point
    2022-04-01T09:04:40.173+00:00

    Hi, for Consul registration I use:

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

    consulClient.Agent.ServiceDeregister(registration.ID).ConfigureAwait(true);
    consulClient.Agent.ServiceRegister(registration).ConfigureAwait(true);

    You can see, I need a uri and a port. In case of docker, this must be the external port, becuase other services should access the service via consul. I can't use the internal docker port, because it is internal and only "visible" for docker.
    The question is, how I can get the internal port (value) and assign this value to registration .Port?

    0 comments No comments

  3. Bruce (SqlWork.com) 63,916 Reputation points
    2022-04-01T17:47:49.047+00:00

    if you want info about the docker container, you use the docker api. in the case of C# , you use the rest api. There is a wrapper

    https://github.com/dotnet/Docker.DotNet

    also, why use a different post in the container?

    0 comments No comments

  4. Wojciech Kusch 1 Reputation point
    2022-04-01T18:36:03.653+00:00

    Hi, the first port ist thr external port. If I have two docker imahes and build the containers, both can have the same internal port. So I need the external port for the distinction between the two containers.

    Can I use the wrapper from dcker.Dotnet nsie my app? I ask because my application runs inside the container - and from this container I need the external port.
    It is easy to use the wrapper from "outside of the system", because I can easily access the docker daemon. But it is true, from inside a container?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.