问题描述
我有一个正在运行的 nginx 容器:# docker run --name mynginx1 -P -d nginx
;
I have a running nginx container: # docker run --name mynginx1 -P -d nginx
;
并通过 docker ps
获取其端口信息:0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp
And got its PORT info by docker ps
: 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp
然后我可以从容器内得到响应(id:c30991a04b2f):
Then I could get response from within the container(id: c30991a04b2f):
docker exec -i -t c3099 bash
curl http://localhost
=> 返回默认的 index.html
页面内容,它可以工作
curl http://localhost
=> which return the default index.html
page content, it works
但是,当我在容器外制作 curl http://localhost:32769
时,我得到了这个:
However, when I make the curl http://localhost:32769
outside of the container, I got this:
curl: (7) 无法连接到本地主机端口 32769:连接被拒绝
我在 docker 版本为 1.9.0 的 Mac 上运行;nginx 最新版
I am running on a mac with docker version 1.9.0; nginx latest
有人知道这是什么原因吗?有什么帮助吗?谢谢
Does anyone know what cause this? Any help? thank you
推荐答案
如果您使用的是 OSX,您可能正在为您的 docker 环境使用 VirtualBox VM.
If you are On OSX, you are probably using a VirtualBox VM for your docker environment.
确保您已转发了您的端口 32769 到您的实际主机(mac),以便从 localhost 可以看到该端口.
这对旧的 boot2docker 或新的 docker 机器有效.
Make sure you have forwarded your port 32769 to your actual host (the mac), in order for that port to be visible from localhost.
This is valid for the old boot2docker, or the new docker machine.
VBoxManage controlvm "boot2docker-vm" --natpf1 "tcp-port32769 ,tcp,,32769,,32769"
VBoxManage controlvm "boot2docker-vm" --natpf1 "udp-port32769 ,udp,,32769,,$32769
(controlvm
如果 VM 正在运行,modifyvm
是 VM 已停止)
(用你的虚拟机名称替换boot2docker-vm
":见docker-machine ls
)
(controlvm
if the VM is running, modifyvm
is the VM is stopped)
(replace "boot2docker-vm
" b ythe name of your vm: see docker-machine ls
)
我建议不要使用 -P
,而是使用静态端口映射 -p xxx:80 -p yyy:443
.
这样,您可以使用固定值进行一次端口转发.
I would recommend to not use -P
, but a static port mapping -p xxx:80 -p yyy:443
.
That way, you can do that port forwarding once, using fixed values.
当然可以通过docker-machine ip vmname
curl http://$(docker-machine ip vmname):32769
这篇关于docker nginx 容器未收到来自外部的请求,连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2