在安裝了一個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的瀏覽器中,輸入這個地址,加上服務的端口即可啓用了。
比如,我在docker中運行了一個nginx的服務,啓動命令
docker run -d -p 8888:80 hub.c.163.com/public/nginx:1.2.1
最後,在瀏覽器中測試即可看到結果: