(C# ASP.NET Core) How to Pull and run MS SQL Server Image in Docker Desktop

In this tutorial we shall learn how to pull and run the docker image of Microsoft SQL Server through the Docker Desktop program. In this same tutorial we shall establish a connection to this instance with the Visual Studio SQL Server Object Explorer.

Categories | About |     |  

Parveen,

Step 1 - Start the Docker Desktop on your PC

First of all start the Docker Desktop on your PC. Ensure that the window opens and that it is in running state. This window causes all the necessary background services to start.

As usual, we shall be using a command prompt to run our commands. So the Docker Window can be closed now, because the background services will continue to run for our requirements.

Video Explanation

Please watch the following youtube video:

Step 2 - Reach the Docker Hub Page for MS SQL Server

Next, let us open the official page for the images of SQL Server.

Microsoft maintains an official page on the hub.docker website for images of SQL Server. Search out for that page and open the first link that you see.

This is the page of the verified publisher as you can see.

You can scroll down this page to locate "How to use this Image" section. They have given a lot of information that you can read later.

For now we shall copy the command for SQL Express Edition, and close the browser.


// Windows PC uses a hat ^ for breaking a line on a 
// long command. Use a slash on linux. 

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=yourStrong(!)Password" ^
 -e "MSSQL_PID=Express" -p 1433:1433 ^
 -d mcr.microsoft.com/mssql/server:2019-latest 

Step 3 - Open a Command Prompt

Next, open a command prompt on your PC.

And paste that command.

IMPORTANT: choose a Strong(!)Password OTHERWISE THE SQL SERVER WILL KEEP EXITING - WILL NOT RUN.

// Windows PC uses a hat ^ for breaking a line on a 
// long command. Use a slash on linux. 

docker run  -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=qw123QE!@4" ^
-e "MSSQL_PID=Express" ^
--rm -p 1433:1433 --name sql1 -h sql1 ^
-d mcr.microsoft.com/mssql/server:2019-latest

This command will automatically pull the image if the image is not already present on your PC. After pulling the image it will automatically launch the container. This makes it very easy for us.

Notice that we have added a few additional parameters

  1. We have set a strong password - a Strong(!)Password is required, OTHERWISE THE SQL SERVER WILL KEEP EXITING - WILL NOT RUN.
  2. The --rm parameter is for ensuring full cleanup when the container is closed.
  3. The --name parameter gives a name to the container. We shall use this name for stopping the container.
  4. The -p command tells us that our SQL Server will listen on localhost port 1433.

Next hit enter to execute this command. It will take a long time to complete, depending on the speed of your internet connection.

Step 4 - Open Visual Studio

Once the container starts running, open Visual Studio.

Use the menu Edit > SQL Server Object Explorer to open the connections window. This window shows all the SQL Server connections that you may have configured on your machine.

Click the add icon to add a new connection. Type Server = localhost,1433. Choose authentication as SQL Server Authentication. Type SA for user name, and the above same password in the password box.

Click connect. Your connection should appear in the SQL Server Object Explorer window!

In the next tutorial we shall connect our HelloDocker ASP.NET Core Application to this SQL Server instance.


This Blog Post/Article "(C# ASP.NET Core) How to Pull and run MS SQL Server Image in Docker Desktop" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.