SQL Server 2017 in a Container – Part 4

In Part 3 I showed how to create a container with default options and how to launch a command prompt and powershell inside the container.

In this blog I will show how setup the network side in order to connect to the container from the container host and a Hyper-v VM running on the container host.

Container Networking

This is a huge topic. Whole books and blog series have been written on this subject alone. So therefore I will just explain what is needed to get external connectivity to the container.

Windows Containers use the Virtual Switch (vSwitch) configured in Hyper-v. This switch provides the connectivity from the containers to the physical network on the physical host.

Nat and Default Switch were created when docker started up. Hyper-V-External-Network is a network I created for my Hyper-v VMs to bind to the physical network card for local and external access.

docker network ls

We can view what networks docker knows about with the docker network ls command. Notice the DRIVER column. Each one had different characteristics and functionality.

Let’s check what network the Demo container is using by default. We never specified what we wanted.

docker container inspect Demo

The output shows a JSON listing of the container configuration. Below is just the network settings.

Notice it is using nat. Let’s inspect nat.

Docker network inspect nat. There is a lot of output so again I have just selected a subset.

The container will have an IP address within this subnet and looking at the picture above, it had allocated

Let’s look inside the container.

docker exec –it Demo cmd

We can run ipconfig /all

Everything is fine. I have a VM where I do I all my development. Let’s see if we can ping it from there.

No we cannot which is going to be a problem. All my testing is done inside the VM so let’s see how we fix that.

Changing Container Network Settings

If we check my VM’s network settings we see that it is using the Hyper-V-External-Network.

It can successfully ping the container host.

docker network ls

Notice that docker already knows about our Hyper-V-External-Network network. Let’s change our Demo container to use it.

Run the following.

  • docker stop Demo
  • docker network disconnect nat Demo
  • docker network connect Hyper-V-External-Network Demo
  • docker start Demo
  • docker container inspect Demo

The container is using our network but no IP address is showing up. Let’s ping the container alias as shown in the picture

ping c0dced8dff89 -4

We have an IP address. Let’s see if we can ping it from inside my VM.


Works great.

In the next blog I am going to show you how to create a container with more options and then how we can connect to it from outside using sqlcmd and SQL Server Management Studio.