Sunday, December 27, 2015

Mapping ports for Tomcat image

How to hit docker tomcat container from your mac

Download and run official tomcat 8 image:
docker run -d -P tomcat:8
This downloads and runs tomcat 8 image in the background (-d option) and maps the container ports to host (-P option). Host here is the VM running boot2docker.

docker ps
This shows container to host port mapping:
PORTS                  0.0.0.0:32768->8080/tcp
This means tomcat is listening on port 8080 of the container which is mapped to port 32768 of the host( boot2docker VM)

This can be verified on the boot2docker host.
Open VirtualBox and double click on the "default" VM to open up a terminal inside it.
curl localhost:32768
You should be able to see the default tomcat html as the response.

Now lets find the ip of the VM by doing ifconfig. It is the one that starts with 192.168.X.X
In my case it was 192.168.99.100
Now that we know the IP, we can do an ssh tunnel from mac's port to the VM's port.
boot2docker has the following credentials as standard:
Username: docker
Password: tcuser

On the mac:
sudo ssh -gNL 80:localhost:32768 docker@192.168.99.100
This will tunnel requests coming to port 80 on the mac to VM's port 32768
Now hit localhost on your browser in mac to see the tomcat page.
Cheers :)