Access Host Services from Containers on Windows Server 2016
Occasionally it is necessary to access services running on the host machine from a container. You may be inclined to use localhost to reach the host, but this will not work as the container itself is localhost. Here is how to access host services from a container running on Windows Server 2016.
This applies to containers running with NAT networking mode. This is the default, so unless you explicitly use something else, this should work for you.
If you read up Windows Container Networking you will find that with NAT network, by default, each container will get a virtual network adapter assigned to an IP in a subnet (by default
172.16.0.0/12). The gateway will be the host machine unless you manually create a NAT network and specify it explicitly. With this knowledge we can retrieve the host address in two ways.
Method 1) Virtual Network Adapter on Host Machine
Open a new CMD or Powershell session and hit
ipconfig. You should now see the vNIC acting as the gateway for containers, having an IP address starting with 172. In the picture below it is the HNS Internal NIC, with IP address
Method 2) Virtual Network Adapter in Container
Similarly, in a running container, the same IP will appear as the gateway. Here is a screenshot from accessing a running container and issuing
From the running container, an application can use this information to get the address
172.20.128.1 and access services on the host machine.