Docker Community Forums

Share and learn in the Docker community.

Host Port mapping visible in the container

(Isabin) #1


I have a docker image that exposes a service at a given/static port, let’s say 80. I am starting multiple containers from this image w/o configuring specific port mappings. I basically want to let the docker infrastructure select, for each container, an available port on the host and map it to the container port 80. I basically end up with the following:

HOST_host:dynamic-port1 -> CONTAINER1_host:80 (Service-Instance1)
HOST_host:dynamic-port2 -> CONTAINER2_host:80 (Service-Instance2)
HOST_host:dynamic-port3 -> CONTAINER3_host:80 (Service-Instance3)

Now, each service instance, which runs in a docker container, needs to advertise its public address during the bootstrap of the container. So, Service-Instance1 advertises HOST_host:dynamic-port1, Service-Instance2 advertises HOST_host:dynamic-port2, and so on. How can each service instance get the host port mapping for “its” port 80?

Thanks in advance.

(Jeff Anderson) #2

Normally, I would expect that if you wanted a service to connect to itself, it would just connect to its own port 80. I’m guessing you are asking the question because you have some requirement that leaves that out as a possibility.

You may need to run a discovery service alongside your container for this level of introspection. The basic idea is you would have something that can talk to docker over its API, and make the port information available that way.