Docker Desktop for Windows: cannot access service on exposed port in windows container mode
This is a currently a known issue on Windows. It's not possible to access a container endpoint from its own host using localhost/127.0.0.1. It is possible using Linux containers today because Docker has included a special workaround that is unique to their Moby/Linux implementation for running Linux containers on Windows.
We're working on a fix for this, but today we recommend working around this by either:
- Accessing container endpoints from a separate host, using the IP address of the host that is running the container, and the exposed port for the container on its host
- OR by accessing the container on the same host, using the container's internal IP address and published port (you can use
docker network inspect <network name>
ordocker exec <container ID> ipconfig>
to get the IP address of the container endpoint itself)
To complete @Kallie-Microsoft post:
docs.docker.com have been updated with a section Limitations of Windows containers for localhost and published ports
Docker for Windows provides the option to switch Windows and Linux containers. If you are using Windows containers, keep in mind that there are some limitations with regard to networking due to the current implementation of Windows NAT (WinNAT). These limitations may potentially resolve as the Windows containers project evolves.
One thing you may encounter rather immediately is that published ports on Windows containers do not do loopback to the local host. Instead, container endpoints are only reachable from the host using the container’s IP and port.
So, in a scenario where you use Docker to pull an image and run a webserver with a command like this:
docker run -d -p 80:80 --name webserver nginx
Using curl http://localhost, or pointing your web browser at http://localhost will not display the nginx web page (as it would do with Linux containers).
In order to reach a Windows container from the local host, you need to specify the IP address and port for the container that is running the service.
You can get the container IP address by using docker inspect with some --format options and the ID or name of the container. For the example above, the command would look like this, using the name we gave to the container (webserver) instead of the container ID:
$ docker inspect \ --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \ webserver