Share via

Running SQL Server + ASP.Net Core in a container on Linux in Azure Container Service on Docker Swarm - Part 2

In previous post, we looked at how to create and run SQL Server Container. I'll build on that and develop an ASP.Net Core application that can interact with database inside this container.

Start with File-->New is Visual Studio 2015 and create an ASP.Net Core application. When all clicks finish, solution structure looks something like this.


Ignore the Hubs folder. As some may have guessed, I am trying to do some SignalR stuff. But you can start with a plain ASP.Net Core application as well.

Now to most important part, how to connect to SQL Server Container?

Connecting to a container is no different than connecting to a SQL Server. From a .Net Core application,a connectionstring needs to be defined and used with a Data Access Library. I am going to use .Net Core's native SQL Server client library. Any one of the many options listed here can be used as well.

Use Nuget Package Manager from the Visual Studio Solution and pull System.Data.SqlClient package. sqldockernuget

Once package is registered, define connectionstring. As in the past, there are many different ways of defining connectionstring. In this post, I am going to define inside my SQL Data Access class.


Important part of this connectionstring is the value of "Data Source". It should be the name as defined by using --name switch when the container was created. In short, it should be name of the SQL Server Container. Use strong password as opposed to the one used above.

Once connectionstring is defined, rest of queries can be implemented the way they are used to be defined. In my example, a INSERT block of C# code looks like below.


With the Docker Support in Visual Studio, It is possible to run application straight from within Container instead of running it from IIS Express.


One can see the docker commands getting executed in the command prompt when you run the application from Visual Studio.


We now have got both SQL Server as well as ASP.Net Core application running inside containers. I am running these containers on my local machine using "Docker For Windows". I am simulating a Linux environment for these containers to run.


I can verify if the SQL Server Container is really able to store the data by querying table from SSMS.


Surely, I do see data getting into table inside SQL Server Container. Just like we did in last post, let's create and push the image for ASP.Net Core application. To create an image, use following docker file and put it in the same location as Visual Studio Solution file.


Run the following command to create docker image.


This image is now create on local machine. As in previous post, use docker push command to push it to  Docker Hub.

In the next post, we'll take this set up and run it inside Azure Container Service using Docker Swarm as an orchestrator.