26

I am running tomcat in my docker container from the Official Repo.

docker pull tomcat

And as per guidelines stated in the homepage I've run the instance and the Tomcat server is started

docker run -it --rm -p 8888:8080 tomcat:8.0

And Tomcat server is available on the port 8888. I am able to get the response in the boot2docker when I execute the following command

curl localhost:8888

But I would like to access the page from my web browser installed in my PC (which is out of the Virtual Box, the one that is installed in my Windows). Can I? If so how?

Gunith D
  • 1,843
  • 1
  • 31
  • 36
JavaTechnical
  • 8,846
  • 8
  • 61
  • 97

5 Answers5

26

You may be missing a port forwarding rule in the VirtualBox settings.

boot2docker vm > settings > Network > NAT adapter > Port forwarding

Note the last entry:

enter image description here

This way you have configured the whole chain: hostPort:virtualBoxPort then virtualBoxPort:containerPort, as shown in the following diagram: enter image description here

tduchateau
  • 4,351
  • 2
  • 29
  • 40
  • 1
    That is great! Thank you :) So, in a similar way to get on working with new ports, we need to add them to the port forwarding rules. Am I right? By the way, what are Host Port and Guest Port? – JavaTechnical Dec 15 '14 at 14:24
  • You can update the start.sh Shell script (provided with the Boot2Docker install) so that the desired port forwarding rules are set up once and for all. See for example https://github.com/docker/docker/issues/4007#issuecomment-34573044. Otherwise you can add them manually as described above. – tduchateau Dec 15 '14 at 15:05
  • `Host port` is the port on your machine (your PC). The `guest port` is the port of the VM (VirtualBox). – tduchateau Dec 15 '14 at 15:06
  • But I put the same port number for both `Guest` and `Host` ports, but still it worked for me! In my example, `8080` is guest port which is to be mapped with `8888` on the host. But, I copied the same as in your screenshot. How could it be possible? – JavaTechnical Dec 16 '14 at 16:37
  • 1
    I just added a diagram for clarification. Hope it's self explanatory enough :-) – tduchateau Dec 16 '14 at 20:20
  • 1
    Just FYI for people not sure how to add it. Start -> virtualbox -> click your box -> Settings -> Network -> Port Forwarding, You can then add the setting above. – Jack Feb 05 '16 at 11:22
8

Seems there's a better answer available since Docker-Tools was introduced a few months ago. If you're not using Docker Tools, take a look at https://docs.docker.com/machine

If you are using Docker Tools,

  1. find the docker machine name with $ docker-machine ls
  2. find the docker IP address with $ docker-machine ip <machine-name>
  3. use the ip address to connect, e.g. $ curl 192.168.99.100
Kay V
  • 3,738
  • 2
  • 20
  • 20
3

run boot2docker ip to get the IP of your docker bridge. Then you can run curl BRIDGE_IP:8888 The IP will be something like 192.168.59.103. With boot2docker the VMs networking stack is not your local host but running on a virtual interface.

Usman Ismail
  • 17,999
  • 14
  • 83
  • 165
  • `Failed to get VM Host only IP Address. Was the VM initialized using boot2docker?` I am getting this error. – JavaTechnical Dec 15 '14 at 04:04
  • I've modified the start.sh file and somehow managed to get the IP address, but still I ain't able to open http://192.168.59.105:8888 in the browser but I am able to get response using `curl` in boot2docker – JavaTechnical Dec 15 '14 at 07:23
  • I saw the same when running from the windows shell, and found out the `boot2docker ip` command must be run from the Git Bash shell. – bnieland Apr 15 '15 at 18:15
1

option -p has argument hostPort:containerPort so in your case port 8080 (inside your docker image) is mapped to port 8888 on the host (your windows box). This means you should be able to find the tomcat on your Windows box by appending :8888 to your URL, just like you did with curl.

geert3
  • 7,086
  • 1
  • 33
  • 49
1

Using docker-machine ls to get the IP of the Virtual Box instance running the container. Then use that with the associated port you exposed, in my case 8080, so it was http://192.168.99.100:8080.

joenova
  • 11
  • 1