Networking with Docker on Windows

2019-03-29 09:45发布

I used this official guide to set up Docker on a Windows 7 machine:

https://docs.docker.com/windows/started/

I successfully pulled an image from the docker hub and I can run my own docker image.

No I am stuck trying to run and access a webserver with docker on Windows. Apparently, behind boot2docker I can't reach my docker container the way I was used to.

Once I added -p 3007:80 to the docker run command, the port forwarding showed up in the container list (docker ps) as 0.0.0.0:3007 -> 80. And with -p 127.0.0.1:3007:80 I get a more meaningful ip address. I cannot, however, reach the container with a browser on the Windows host.

Moreover, docker inspect does not reveal an ip address for the running container (which also seems wrong).

I also tried --net=host to no avail.

2条回答
一纸荒年 Trace。
2楼-- · 2019-03-29 10:02

Things changed more than just a little by now: Docker supports Windows natively and Boot2Docker or Docker Toolbox aren´t needed anymore. You should start with Windows 10 (or Windows Server 2016) now.

Throughout my Docker on Windows journey I developed a best practice to check, if a webserver is accessible:

  1. Run your container (e.g. with your mentioned docker run -d -p hostport:containerport imagename)
  2. Shell into the container (e.g. with a Windows Container: docker exec -it containerName powershell or docker exec -it containerName bash, if you use Linux containers) and check, if iwr http://localhost:80 -UseBasicParsing gives you a good looking HTML and HTTP Status 200
  3. If 2. went fine (and only then), check the IP of your container (e.g. with docker network inspect dockerNetworkName, which´ll give you all your containers data including the internal IP addresses) and point your Browser to http://containerIP:containerPort.
  4. If 3. went fine (and again: only then), check if you could reach your webserver with http://localhost:containerPort (this maybe doesn´t work for Windows containers, because the localhost loopback isn´t working - although it could work on newer versions of Windows 10/Server 2016)
  5. Final step (if 1., 2. & 3. went fine, 4. optional), try to lookup your webserver from outside the Windows host (e.g. if you´re running a Windows host inside Virtual Box, mind the port forwarding configuration!)
查看更多
三岁会撩人
3楼-- · 2019-03-29 10:07

Things changed a little with the introduction of Docker Toolbox. Now you do not directly interact with boot2docker, but instead use docker-machine. Although boot2docker still exists as VM there is no CLI-Tool any longer. It was replaced by Docker Machine.

Thus you should be able to get hold of the VM's IP address by typing: docker-machine ip <machine-name>. If you do have the default installation your machine name will be default.

With docker-machine active you can have a look which VM is currently active. With that name you can also use docker-machine inspect <machine-name>.

You can find more about Docker Machine in the official docs.

查看更多
登录 后发表回答