问题描述
我一直在创建一个Web服务器作为Docker容器,并试图在容器外部访问它。但我不能。我正在做的是。
- ubuntu的图像:
docker pull ubuntu:14.04
- 启动一个容器:
docker -t -i -p 49200:2375 [image-id]
并且在新启动的容器中安装apache2后,我可以ping容器中的apache2服务器,也可以在容器终端中查看结果,但在主机上没有运气。我已经尝试了很多次。 (我在Windows上使用虚拟框)
屏幕截图:
问题:
- docker命令和端口号来创建容器。
- apache配置有什么问题吗? (我在默认模式下使用apache没有任何配置)
- 我的虚拟框或我的电脑上的端口有什么问题。我的网络知识很低。
有人可以帮助!!!
问题1 :Apache默认端口
2375
端口不是apache默认端口,它是docker守护程序默认端口,命令应为
docker -t -i -p 49200:80 [image-id]
问题2 :不同机器中的端口
49200:80
这是从内部端口 80的映射
到Docker主机 49200
,docker主机实际上是你的虚拟机VM(猜测是你的boot2docker shell)
在虚拟机VM控制台中,您可以
curl localhost:49200
图片端口转发规则正在帮助您的端口转发到您的虚拟机主机,因为您是MacOS,您映射 49200
到MacOS 49200
以及
所以在你的MacOS shell控制台,你也可以
curl localhost:49200
如果您知道virtualbox VM(docker host)IP地址,例如 192.168.59.103
作为boot2docker的默认值,那么您可以
curl 192.168.59.103:49200
如果要通过
172.17.0.7
访问apache docker容器,请记住这是私人网络内部,您需要启动另一个容器,如
docker -t -i busybox bash
#卷曲172.17.0.7:80
摘要
摘要
docker -p
将端口转发到docker主机
虚拟机设置中的端口转发规则是将您的VM端口转发到VM主机
- apache docker容器:172.17.0.7:80
- docker host = virtualbox VM:192.168.59.103:49200
- virtualbox host = MacOS:xxxx:49200
I have been creating a webserver as Docker container and trying to access it outside the container. But I can't. What I am doing is.
-Pulling a ubuntu image:
docker pull ubuntu:14.04
-Starting a container:
docker -t -i -p 49200:2375 [image-id]
and after installing apache2 in newly started container I can ping the apache2 server inside container, I can also do curl and see results in container terminal, but no luck outside on host machine. I have tried many times. (I am using Virtual Box on windows)
Screen shots:
Questions:
- Am I using right docker commands and port numbers to create container.
- Is there anything wrong in apache configuration. (I am using apache in default mode without any configurations)
- Is there anything wrong with ports on my virtual box or on my pc. I have very low knowledge of networking.
Can someone help!!!
解决方案 Problem 1: Apache default port
2375
port is not the apache default port, it is the docker daemon default port, the command shall be
docker -t -i -p 49200:80 [image-id]
Problem 2: port in different machine
49200:80
This is the mapping from internal port 80
to docker host 49200
, the docker host is actually your virtual box VM (guess is your boot2docker shell)
In your virtual box VM console, you can
curl localhost:49200
The picture "Port Forwarding rules" are helping your port forward to your virtualbox host, for you it is MacOS, where you mapped 49200
to MacOS 49200
as well
So in your MacOS shell console, you can also
curl localhost:49200
If you know the virtualbox VM (docker host) IP address, for example it is 192.168.59.103
as default for boot2docker, then you can
curl 192.168.59.103:49200
If you want to access the apache docker container via 172.17.0.7
, remember this is the private network inside, you need to start another container like
docker -t -i busybox bash
# curl 172.17.0.7:80
Summary
docker -p
is port forwarding your internal port to docker host"Port forwarding Rules" in virtualbox setting is port forwarding your VM port to VM host
- apache docker container: 172.17.0.7:80
- docker host=virtualbox VM: 192.168.59.103:49200
- virtualbox host=MacOS: xxxx:49200
这篇关于从Host机Web浏览器访问Docker容器内的apache2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!