How to get the mapped port on host from a docker container?

前端 未结 3 1818
南方客
南方客 2021-02-02 15:59

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

3条回答
  •  醉梦人生
    2021-02-02 16:14

    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:

    • etcd
    • consul
    • zookeeper

    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.

提交回复
热议问题