解決windows系統沒法對docker容器進行端口映射的問題

一、問題:docker

  在Windows家庭版下安裝了docker,並嘗試在其中運行jupyter notebook等服務,但映射完畢以後,在主機的瀏覽器中,打開localhost:port沒法訪問對應的服務。windows

二、問題出現的緣由:瀏覽器

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.app

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。this

三、解決方法:spa

  經過命令code

  docker-machine ip default   # 其中,default 是docker-machine的name,能夠經過docker-machine -ls 查看
ip

找到這個Linux的ip地址通常狀況下這個地址是192.168.99.100而後在Windows的瀏覽器中,輸入這個地址,加上服務的端口便可啓用了。ci

好比,首先運行一個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

相關文章
相關標籤/搜索