18

I want to run a task in some docker containers on different hosts. And I have written a manager app to manage the containers(start task, stop task, get status, etc...) . Once a container is started, it will send an http request to the manager with its address and port, so the manager will know how to manage the container.

Since there may be more than one containers running on a same host, they would be mapped to different ports. To register a container on my manager, I have to know which port each container is mapped to.

How can I get the mapped port inside a docker container?

There's an solution here How do I know mapped port of host from docker container? . But it's not applicable if I run container with -P. Since this question is asked more than 1 year ago, I'm wondering maybe there's a new feature added to docker to solve this problem.

0

3 Answers 3

17

You can also you docker port container_id

The doc

https://docs.docker.com/engine/reference/commandline/port/

examples from the doc

$ docker port test 7890/tcp -> 0.0.0.0:4321 9876/tcp -> 0.0.0.0:1234 $ docker port test 7890/tcp 0.0.0.0:4321 $ docker port test 7890/udp 2014/06/24 11:53:36 Error: No public port '7890/udp' published for test $ docker port test 7890 0.0.0.0:4321 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but this can not be run inside a container. Is it possible to get the port inside a container?
5

i share /var/run/docker.sock to container and get self info

docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock alpine:latest sh 

in container shell

env //get HOSTNAME curl --unix-socket /var/run/docker.sock http://localhost/containers/3c6b9e44a622/json 

the 3c6b9e44a622 is your HOSTNAME

1 Comment

Only problem here is that if your service gets compromised you are creating a new vector of attack on your entire docker system... I think I would rather make a dedicated docker container to give you info for you container... because only your container would communicat with it, not outside world
4

Once a container is started, it will send an http request to the manager with its address and port

This isn't going to be working. From inside a container you cannot figure out to which docker host port a container port is mapped to.

What I can think about which would work and be the closest to what you describe is making the container open a websocket connection to the manager. Such a connection would allow two ways communication between your manager and container while still being over HTTP.


What you are trying to achieve is called service discovery. There are already tools for service discovery that work with Docker. You should pick one of them instead of trying to make your own.

See for instance:


If you really want to implement your service discovery system, one way to go is to have your manager use the docker event command (or one of the docker client librairies). This would enable your manager to get notified of containers creations/deletions with nothing to do on the container side.

Then query the docker host to figure out the ports that are mapped to your containers with docker port.

1 Comment

gliderlabs/registrator is a good choice for Docker container level service discovery.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.