解决无法对docker容器进行端口映射的问题

本文解决了在Windows环境中使用Docker运行服务时,通过浏览器无法访问的问题。详细介绍了Windows下Docker的工作原理,并提供了如何正确配置及访问服务的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在安装了一个Windows下安装了docker,并尝试在其中运行Nginx、owncloud等服务,但映射完毕之后,在主机的浏览器中,打开localhost:port无法访问对应的服务。

用百度尝试了下,始终找不到答案,难道这个问题大家都没遇到过么?于是使用Google,并用英文检索,一会就找到了对应的答案。

The reason you’re having this, is because on Linux, the docker daemon (and your containers) run on the Linux machine itself, so “localhost” is also the host that the container is running on, and the ports are mapped to.

On Windows (and OS X), the docker daemon, and your containers cannot run natively, so only the docker client is running on your Windows machine, but the daemon (and your containers) run in a VirtualBox Virtual Machine, that runs Linux.

原来,docker是运行在Linux上的,在Windows中运行docker,实际上还是在Windows下先安装了一个Linux环境,然后在这个系统中运行的docker。也就是说,服务中使用的localhost指的是这个Linux环境的地址,而不是我们的宿主环境Windows。我们可以通过命令

docker-machine ip default

找到这个Linux的ip地址,一般情况下这个地址是192.168.99.100,然后在Windows的浏览器中,输入这个地址,加上服务的端口即可启用了。

微信截图_20170427183257

比如,我在docker中运行了一个nginx的服务,启动命令

docker run -d -p 8888:80 hub.c.163.com/public/nginx:1.2.1

微信截图_20170427183247

最后,在浏览器中测试即可看到结果:

微信截图_20170427183208

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值