简体   繁体   中英

How to access tomcat docker container running in VMWare workstation?

I know this question sound as a repeat question, but I couldn't find other that suit to my requirement. So I just started to use docker for developing tomcat app.

After installing docker in Ubuntu 16 inside VMWare Workstation , and downloading tomcat-docker image, I want to start the tomcat container using START command instead of RUN .

I use START command because I don't want to stuck within STDOUT, and able to continue using Linux command line. So I start the container by using below command

 $ docker start name_of_container

Then I issue inspect command

 $ docker inspect name_of_container

I manage to see that my tomcat instance is using ip: 172.17.0.2 . So I did curl like:

 $ curl 172.17.0.2:8080 | head

And I got my HEAD tags printed perfectly, which means that I successfully instantiate my tomcat.

My question is: how can I access my tomcat from my browser in Windows? how can I create a port forwarding in VMWare to enable me to access ip 172.17.0.2??

Thanks

First off, you can use the RUN command without loosing your terminal with the -d option. It starts the container in dettached mode and tomcat will be in the background.

Second, when you run your container, you will need to map its exposed port to another port in your VM, with the -p option.

So the run command should look like this :

docker run -d -p 8000:8080 tomcat

Where 8000 is an open port on your VM, and 8080 is port which tomcat is listening to in your container. The traffic coming to your VM on the port 8000 will be redirected to the port 8080 that the tomcat container exposes.

Lastly, you will need to find your VM's IP, not your container's IP.

An ifconfig on the VM should suffise for that. You should be able to ping it from your host machine (so it can be accessible from a browser later on). So if your VM's IP turn out to be 192.168.1.50 (for example), you will need to type this in your browser :

192.168.1.50:8000

Here 8000 is just an example. You can use 8080 too for less confusion in the RUN command :

docker run -d -p 8080:8080 tomcat

If using docker create to create the container, specify the ports to publish to the host with --publish :

docker create --publish 8080:8080 --name my-tomcat tomcat

You can then start with:

docker start my-tomcat

Port 8080 on your host will now send traffic to port 8080 of your container.

You should be able to reach http://ip-of-vm:8080 in your browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM