Tuesday, August 11, 2015

Public access to docker container

Let's say docker container running some application along with ssh daemon is launched on base dockerhost (could be Linux, CoreOS etc VMs).

Now you are inside docker container and you need to know the port of external host to which container's ssh port is mapped.

This query should give you the result:

curl -s http://$(route | grep default | awk '{print $2}'):4243/containers/json | grep $(hostname) | awk -F":" '{$NF=$(NF-1)=""; print $0}'| awk '{print $NF}'| awk -F"," '{print $1}'

Please note that docker host name is not hardcoded in the query. It is derived from network configuration of container itself.