Incredibly, I could not find any answer of how to do it anywhere on the web. First I thought Docker should have a configuration for it or a different process to create and use custom networks on Windows, but the truth is that Docker has nothing to do with this issue. It is merely a matter of Windows settings.
What I am saying is that there is a much simpler way to use a “bridged” networked container (Linux type) in Windows without any “dirty trick”, “special driver” or “hack” as many crazy answers I found suggested.
Here is how to do it right: First, create a custom network on Docker exactly as if you were doing it on Linux. Example:
docker network create -d bridge --subnet 172.168.0.0/16 mynet
Once you do that in Linux, your new network is automatically bridged to your NIC, allowing you to create any container with the keywords “--network mynet --ip 172.168.0.1
” (where IP can be any IP in the range of the network you created). It will define your container as part of the network and set it to use the specified IP address, allowing you to create and simulate process isolation, test intra-network processes, use custom local domains via host
file, etc.). In Windows, however, it simply is not possible because the Hyper-V doesn’t offer Bridged option for Linux containers, which simply mutes the Bridge element created by docker network command. It will be there, but will behave as a NIC without a cable connected to it (no communication in any direction).
To connect the newly created Docker Bridge in Windows is simpler than you may think. Basically, every time you want to set a container to respond from your custom network, using a custom IP, the specific IP address must exist in the Docker Network Adapter, commonly named as vEthernet (DockerNAT). Here how to do it:
1 ) Go to Control Panel -> Network and Internet -> Network Connections
2 ) Right Click on vEthernet (DockerNAT) and select Properties
3 ) Select Internet Protocol Version 4(TCP/IPv4) and click Properties
4 ) Give to the adapter a static IP address that is valid on your current real network. Make sure the DNS is also valid so the container will be able to resolve domains on the Web
5 ) Once set, click Advanced, then click on Add and then enter the IP you want to include in the list. The Subnet mask should autofill as soon as you click on it, based on the IP address you’ve entered (of course, you can change it if needed). Repeat this process for every IP you intend to use. Each single IP you set for a container must exist in the list of IP Addresses of the Adapter. You can add as many IP’s you need, from the same or different networks. It will be used internally only to allow your computer to access the container using one of those specific IPs.
Once you complete this step, you can start using your custom network as if you were on Linux. No difference.
As an example, try installing the Portainer to run from your custom network. Here’s how:
First, create the require Portainer volume (step only needed on Windows):
docker volume create portainer_data
Then create the Portainer container using:
docker run -d -p 9000:9000 --name portainer --network mynet --ip 172.168.0.254 --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
Noticed the keywords --network
and --ip
? You can even set a custom local domain (for instance, docker.local
) in the hosts
file that points to the IP address 172.168.0.254
which will allow you to access the local Portainer via http://docker.local:9000
(or http://172.168.0.254:9000
).
That’s it! You are done. All working exactly as it works on Linux. Have fun!
A very simple process that works flawlessly. No need to mess with the Hyper-V Manager configuration, add external devices or any other “hack”. Nothing more than the old and simple network configuration process that most people nowadays appear to have forgotten still exist.
P.S.: I wonder why such information cannot be found anywhere in the Docker documentation or even in the official forums?