解決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

 

 

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM