Create a SQL Database Windows Docker Container associated with an ASP.NET Core 3.1 Blazor App
This article shows how to use SQL Server Express in a Windows Docker container communicating with an ASP.NET Core 3.1 Blazor app running on the host computer.
The following is assumed:
1) You have .NET Core 3.1 installed on your computer. Go to https://dotnet.microsoft.com/download/dotnet-core to download and install .NET Core 3.1.
2) You have Docker for Windows installed on your computer. Refer to https://docs.docker.com/docker-for-windows/install/ for instructions on how to install “Docker Desktop on Windows”.
Let’s get started.
Right-click on the whale docker icon in the tray and make sure it is in Windows Container mode. This is the case if it displays “Switch to Linux containers…”
Create a Blazor Server Project
Launch Visual Studio 2019 (Community)

Click on “Create a new project”
Create a new project window opens as below

Click on Blazor App (from ‘Recent project templates’ on left)
On the Configure your new project Give a project name : BlazorSqlDockerDemo and Click Create


Click Change (Authentication) => Individual User Accounts and Click OK to close the modal window and Create (on the to create the project

Once the project is created, click BlazorSQlDockerDemo green arrow in VS 2019

A terminal window comes up with

It suggests that the internal kestrel server is up on Port 5001 (https) and the browser comes up with

Close your browse and stop the web server by hitting CTRL + C
Open Startup.cs in an editor and look at the ConfigureServices() method. The following code confirms that SQL Server is being used:
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”)))
The connection string is being read from the appsettings.json file, which looks like this:
{
“ConnectionStrings”: {
“DefaultConnection”: “Server=(localdb)\\mssqllocaldb;Database=aspnet-BlazorSqlDockerDemo-1916963F-8BF1-4E14-8184-836828CE0856;Trusted_Connection=True;MultipleActiveResultSets=true”
},
“Logging”: {
“LogLevel”: {
“Default”: “Information”,
“Microsoft”: “Warning”,
“Microsoft.Hosting.Lifetime”: “Information”
}
},
“AllowedHosts”: “*”
}
Add the following code to the Configure() method in the Startup.cs file right after app.UseStaticFiles();
using (var srvc = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = srvc.ServiceProvider.GetService<ApplicationDbContext>();
context.Database.Migrate();
}
The above code will process any outstanding Entity Framework migrations.
We shall now download a suitable Docker image for SQLServer Express. Run the following command from a terminal window:
docker pull microsoft/mssql-server-windows-express:2017-latest
This image is about 5 GB in size, so it will take some time to download. Once fully downloaded, then create a container from the Docker image with the following command:
docker run -d -p 8080:1433 --env sa_password=Sql!Expre55 --env ACCEPT_EULA=Y microsoft/mssql-server-windows-express
The above Docker command will create an image with the following parameters:
– inside the container, the SQL Server database will listen on its default port number 1433. This port number is mapped to port 8080 on the host computer
– sa password is Sql!Expre55
– environment variable ACCEPT_EULA is set to Y
Check that the SQL Server container is running by typing the following Docker command in a terminal window:
docker ps
This should show a message that looks like this:

According to the above message, the container status is Up 24 minutes, which means that it is indeed running.
Now let’s try to connect to it from our Blazor application. To do this, we will need to get the internal IP address of the container. Identify the first three characters of your Container ID. In the above example it is 1c2. This will be used to obtain the container IP address by executing the following command in a terminal window:
docker inspect 1c2
Note: You will need to replace 1c2 above with the first three characters of your Container ID.
A JSON formatted listing of container parameters will be shown. Identify the value for Networks >> nat >> IPAddress. In my case, the container IP address is “172.20.135.124”:

Edit appsettings.json and change the connection string with key DefaultConnection so that it has a value similar to the following:
Data Source= 172.20.135.124.55;User ID=sa;Password=Sql!Expre55;Connect Timeout=30;Database= BlazorSqlExpress;
Note: The IP address should match the one you identified for the container earlier on.
The SQL Server database listens on port 8080 on the host machine. This means that we can have an alternative connection string that looks like this:
Data Source=localhost,8080;User ID=sa;Password=Sql!Expre55;Connect Timeout=30;Database= BlazorSqlExpress;
After saving the appsettings.json file, you can test your web application. Click the green arrow 
If all goes well, you will see a message that indicates that the web server is listening on port 5001. Point your browser to http://localhost:5001. The same web page will appear as before. Click on the Register link on the top right side.
I entered an Email, Password and Confirm password then clicked on the Register button. It then came up with the following Register Confirmation window which asks to click a link to confirm account


Clicking the link above shows the following view:

Let us directly access the container and list all the databases that exist. Using the first three characters of the Container ID, run the following Docker command in a separate terminal window:
docker exec -it 1c2 sqlcmd
The –i switch is for interactive and –t is for pseudo-TTY.
You will see a window that looks like this:

Enter the following pair of commands to display existing databases:
EXEC sp_databases go
The output will look like this:

You can, execute this command to see only base tables:
SELECT * FROM BlazorSqlExpress.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ‘BASE TABLE’; go
Enter these commands to display the content of the AspNetUsers table:
SELECT * FROM AspNetUsers go
To exit the sqlcmd terminal window type exit and hit ENTER.
Follow Me Here
14 Responses
I have read so many content on the topic of the blogger lovers except this piece of writing is truly a good piece of writing, keep it up. Queenie Alf Veedis
Happy that it helped.
You can definitely see your enthusiasm within the article you write. Corny Rodrique Flem
Hurrah! After all I got a weblog from where I can actually get helpful information concerning my study and knowledge. Marla Horton Obie
I am honored and appreciated for your valuable information. Your articles are very nice.
I like your website very much. It has a very nice design. You put a lot of effort into your visitor. very successful good blogs in your article
I’m glad I saw your page, it’s very informative, you’re really great, please continue like this.
I am honored and appreciated for your valuable information. Your articles are very nice.
I like your website very much, the explanations are very clear and simple, I wish your page to improve, goodbye, love and respect.
whoah this blog is great i really like studying your articles.
Some genuinely interesting info, well written and generally user genial. Milo Geery
Spot on with this write-up, I absolutely believe that this site
I used to be recommended this website by way of my cousin. I’m
Having read this I believed it was rather informative. I appreciate you taking the time and energy to put this content together. Kyle Ciulla