Thursday, December 19, 2019

Exposing a port from running docker container

Following steps were done on Windows Docker Quick Start installation.

Run a network service in a new port (which was not exposed during starting of the docker container)
docker exec -it container_name /bin/bash
nohup python -m http.server 8000 &
exit

Expose the port using iptables in the docker host.

docker-machine ssh

sudo iptables -t nat -A  DOCKER -p tcp --dport 8001 -j DNAT --to-destination $(docker inspect -f '{{ .NetworkSettings.IPAddress }}' container_name):8000

exit

Test the connection to the newly exposed port from your local machine.
(I used 192.168.99.100 because I was testing this on Docker Quick Start installation on Windows machine)

curl -s http://192.168.99.100:8001/