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

1、问题:

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

2、问题出现的原因:

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。

3、解决方法:

  通过命令

  docker-machine ip default   # 其中,default 是docker-machine的name,可以通过docker-machine -ls 查看

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

比如,首先运行一个docker 容器:

  docker run -it -p 8888:8888 conda:v1

其中,conda:v1是我的容器名称。然后在容器中开启jupyter notebook 服务:

  jupyter notebook --no-browser --port=8888 --ip=172.17.0.2 --allow-root

其中的ip参数为我的容器的ip地址,可以通过如下命令获得:

  docker inspect container_id

最后在windows浏览器中测试结果:

  http://192.168.99.100:8888

 

 

 

  

 

posted @ 2018-03-31 17:44  blackx  阅读(17451)  评论(7编辑  收藏  举报