In this post I will demonstrate how to put together a .NET Core Web App Docker development environment with a MSSQL Docker development environment using Visual Studio. When I started researching how to put together a .NET Core Web App Docker development environment, I thought it will be something like disused in my previous posts about docker like Docker Magento 2 or Docker Wordpress. But it turned out that it’s much easier while using Visual studio.
First I will show a step by step guide how to establish a .NET Web App Docker development environment using visual studio. You will see how easy it is, compared to other development environment with Docker.
First of all, we need to create a .NET Core Web App sollution, which is located under Visual C# -> .NET Core menu
Then you will be asked to project template. Let’s say we will go with the MVC. At this step is important to check the “Enable Docker Support” and don’t forget also to choose “Linux” from the OS dropdown (both required input controls are marked in the image below)
At this point we already have a prepared solutionthat is compatible with Docker. It contains you Web application project and a docker-compose section that is needed to deploy our .NET Core web app project into a Docker container. Earlier I told you that it’s all preaty easy. Well, It’s so easy that all you need to do know is press the F5 button (a.k.a. click on the run button marked in the image below).
And that’s pretty much it for the first step of enabling .NET Core Web App Docker development environment. Told ya it’s easy 🙂 If you now look into the Kitematic, you will see that our container is running (image below).
Forgot to mention that visual studio will automatically bring up you defaul web browser with the URL address to the running web server in the Docker container.
Now that the .NET Web App Docker development environment is up and running, let’s so to the second step and connect it with a MSSQL database.
We need to tell Docker to initialize a container for holding a MS SQL database server. It can be achieved by editing docker-compose.yml with the following content:
version: '3' services: webapplication1: image: webapplication1 build: context: ./WebApplication1 dockerfile: Dockerfile db: image: "microsoft/mssql-server-linux" environment: SA_PASSWORD: "TestPassZXY017+" ACCEPT_EULA: "Y" ports: - "1433:1433"
As you can see above, we defined the db container with a database password TestPassXXX and an exposed port 1433 so that we can access the db directly from out computer. Now just press F5 to start the Visual Studio project. In the ime below you can see a new running container which contains a MSSQL server.
As mentioned before, we exposed the port 1433 to our computer, so the MS SQL server can be accessed by SQL Server Management Studio. Example of a connection string is visible in the following image.
The username is sa and the password (as defined in the docker-compose.yml) is “TestPassZXY017+“. Now just create a database, let’s say with the name of dean to test the connection with the web server. Now let’s test a connection between a .NET Core Web App and MSSQL. Open project’s file Controllers/HomeController.cs and add the following code inside the Index method:
//CONNECTION STRING TEST - BEGIN SqlConnection cnn; var connection = @"Server=db;Database=dean;User=sa;Password=TestPassZXY017+;"; cnn = new SqlConnection(connection); try { cnn.Open(); cnn.Close(); } catch (Exception ex) { } //CONNECTION STRING TEST - END
Also put a breakpoint on the line ‘cnn.Open();‘ to see if the connection to the database is going to establish. Well, that’s all… just press F5 and have and live ever happily after 😉
Don’t have time to go through the whole process of creating a .NET Core Web App Docker development environment with a MSSQL Docker development environment? I put my solution on github project Docker .NET Core Web application with MS SQL connection, so you can just press F5 and and live ever happily after 😉
If you don’t want to use Visual studio to accomplish this task, you can take a look at this tutorial: aspnet-mssql-compose