解決無法對docker容器進行端口映射的問題

在安裝了一個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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章