简体   繁体   English

如何访问在VMWare工作站中运行的tomcat docker容器?

[英]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. 所以我才刚刚开始使用docker开发tomcat应用程序。

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 . VMWare Workstation中的Ubuntu 16中安装docker并下载tomcat-docker映像后,我想使用START命令而不是RUN启动tomcat容器。

I use START command because I don't want to stuck within STDOUT, and able to continue using Linux command line. 我使用START命令是因为我不想卡在STDOUT中,并且能够继续使用Linux命令行。 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 . 我设法看到我的tomcat实例正在使用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. 而且我的HEAD标签打印得很好,这意味着我成功实例化了tomcat。

My question is: how can I access my tomcat from my browser in Windows? 我的问题是:如何从Windows中的浏览器访问tomcat? how can I create a port forwarding in VMWare to enable me to access ip 172.17.0.2?? 如何在VMWare中创建端口转发以使我能够访问ip 172.17.0.2?

Thanks 谢谢

First off, you can use the RUN command without loosing your terminal with the -d option. 首先,您可以使用RUN命令,而不会通过-d选项丢失终端。 It starts the container in dettached mode and tomcat will be in the background. 它以分离模式启动容器,并且tomcat将在后台。

Second, when you run your container, you will need to map its exposed port to another port in your VM, with the -p option. 其次,在运行容器时,需要使用-p选项将其公开端口映射到VM中的另一个端口。

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. 其中8000是VM上的开放端口,而8080是tomcat在您的容器中侦听的端口。 The traffic coming to your VM on the port 8000 will be redirected to the port 8080 that the tomcat container exposes. 在端口8000上进入您的VM的流量将被重定向到tomcat容器公开的端口8080。

Lastly, you will need to find your VM's IP, not your container's IP. 最后,您需要找到虚拟机的IP,而不是容器的IP。

An ifconfig on the VM should suffise for that. VM上的ifconfig应该足以满足要求。 You should be able to ping it from your host machine (so it can be accessible from a browser later on). 您应该能够从主机上对其执行ping操作(以便以后可以通过浏览器对其进行访问)。 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 : 因此,例如,如果您的VM的IP为192.168.1.50,则需要在浏览器中输入以下内容:

192.168.1.50:8000 192.168.1.50:8000

Here 8000 is just an example. 这里8000只是一个例子。 You can use 8080 too for less confusion in the RUN command : 您也可以在运行命令中使用8080,以减少混乱:

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指定要发布到主机的端口:

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. 主机上的端口8080现在会将流量发送到容器的端口8080

You should be able to reach http://ip-of-vm:8080 in your browser. 您应该可以在浏览器中访问http://ip-of-vm:8080

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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