In the previous tutorial, we saw how to install Docker and Minikube on a Windows machine without using third-party tools like Docker Desktop or Rancher Desktop.
The next step is to run the application within Docker containers and access the local machine's SQL Server, PostgreSQL (pgAdmin), or other tools from inside these Docker containers. In this tutorial, I will guide you on how to set up the connection between Docker and your local machine. Let's get started!
First, you need to get the IPv4 address of WSL (with Hyper-V firewall enabled) by using the Windows Terminal and entering the following command:
ipconfig
It provides a list of IPv4 and IPv6 addresses for different connections such as LAN, Ethernet adapter, Wireless LAN, and WSL. We are interested in the WSL IPv4 address shown below.
Windows IP Configuration
Ethernet adapter vEthernet (WSL (Hyper-V firewall)):
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : ****::****:****:****:****%59
IPv4 Address. . . . . . . . . . . : 172.27.80.1
Subnet Mask . . . . . . . . . . . : 255.255.***.0
Default Gateway . . . . . . . . . :
The IPv4 address 172.27.80.1 for WSL needs to be whitelisted in the Windows etc/hosts file.
Go to the Windows location C:\Windows\System32\drivers\etc\ and drag/drop the hosts file onto the desktop, open it in your editor (e.g., Notepad) and append it by pasting the two lines below:
#Docker
172.27.80.1 host.docker.internal
172.27.80.1 gateway.docker.internal
P.S. Ensure, the IPv4 Address which is being obtained using ipconfig should be used, in my case it's 172.27.80.1
Drag and Drop the updated hosts file to its original windows location :
C:\Windows\System32\drivers\etc\
IIt is advisable to restart the Windows laptop and start the Docker service again by using the command on the Ubuntu terminal:
sudo service docker start
The local SQL server can now be accessed within Docker container using Server name "host.docker.internal,1433" and SQL Connection String will look like this -
Data Source=host.docker.internal,1433;Initial Catalog=DBName;......;
If Local SQL Server is unable to connect, you would need to allow the local SQL server to accept the remote connection by following this link.
Happy Coding!