Docker external port

29309600 1 Reputation point

Hi, I have an 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!

A set of technologies in the .NET Framework for building web applications and XML web services.
3,758 questions
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.
9,439 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce ( 48,481 Reputation points

    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. 29309600 1 Reputation point

    Hi, for Consul registration I use:

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


    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 ( 48,481 Reputation points

    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

    also, why use a different post in the container?

    0 comments No comments

  4. 29309600 1 Reputation point

    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