SQL Server 2017 Docker Container And Web API In .Net Core

In this article, we will be looking at how to use SQL Server 2017 Docker image.

Introduction

 
This article will discuss the following:
1. How to locate SQL Server 2017 image and download it locally
2. Execute the SQL Server 2017 docker container locally
3. Use the SQL Server 2017 hosted inside the Docker container for local .NET Core Web API development
 
Prerequisites
  • Windows 10
  • Visual Studio 2017
  • Microsoft SQL Server Management Studio 18
  • Docker for Windows (Linux containers)
  • Client: Docker Engine - Community
    • Version: 09.2
    • API version: 39
  • Server: Docker Engine - Community
  • Engine:
    • Version: 09.2
    • API version: 39 (minimum version 1.12)

It would be good to have prior knowledge of:

Source Code Repository
 
https://github.com/vishipayyallore/speaker_series_csharp_dotnetcode/tree/master/SQLServer2017DockerWebApi/College.Services
 
Assumption
 
This article assumes that you have the basic knowledge of Docker, SQL Server Management Studio, VS 2017, EF Core 2.1, and Web API using .NET Core 2.1.
 
Motivation
  • Cross-platform
    Microsoft SQL Server 2017 is now available on multiple platforms: Windows, Linux, and Docker.

  • Fast installation
    Getting SQL Server’s docker image is as simple as running a docker image pull.

  • Cost-effective
    Containers are much cheaper.

  • Different versions/Multiple instances
    We can start as many instances on an On-premise Server/Laptop as we want. Each container will be independent (fresh and clean) and tear them back down when we are done.

  • Speed
    The speed and efficiency benefits of Docker and containerizing apps are available to SQL Server 2017 Docker container too.

  • Persistence
    We can use volume mounts to store .mdf and .ldf files outside the container. That way those .mdf and .ldf files will be stored on the persistence hard disk. Even when the container is removed that data will be safe as it is hosted outside the container.

Locating the SQL Server 2017 image and pulling it locally

 
We can visit here to find the image we would like to pull to our local laptop. In this article, we will be using “mcr.microsoft.com/mssql/server:2017-latest”.
 
First, let’s verify the list of available images on our laptop by executing the below-mentioned command using the command line.
 
docker images
 
From the command prompt, please execute the below-mentioned command to pull the SQL Server 2017 image locally. We need to wait for a couple of minutes for the image to be pulled locally.
 
docker pull mcr.microsoft.com/mssql/server:2017-latest
 
SQL Server 2017 Docker Container And Web API In .Net Core
 
Let’s verify that the SQL Server 2017 image was successfully pulled locally. We can see that the image was pulled and its size is 1.33 GB.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 

Executing SQL Server 2017 container locally without Volume mount

 
We will be executing the SQL Server 2017 container without volume mount. The effect of this will be, whenever the container is deleted all the data stored inside the SQL Server database(s) will be lost. Execute the below-mentioned command inside the command line. Ensure that port 1433 is available.
 
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Sample123$" -p 1433:1433 --name sqlserver2017withoutmount -d mcr.microsoft.com/mssql/server:2017-latest
  • -e 'ACCEPT_EULA
    Sets the ACCEPT_EULA variable to ‘Y’ to confirm the acceptance of the End-User Licensing Agreement.

  • -e 'SA_PASSWORD
    Password for login into SQL instance using sa username.

  • -p 1433:1433
    Map a TCP port on the host environment (first value) with a TCP port in the container (second value). In this example, SQL Server is listening on TCP 1433 in the container and this is exposed to the port, 1433, on the host.

  • --name
    Specifies a name for the container rather than a randomly generated one.
Also, execute docker ps -a to verify that the container was successfully created and running.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 

Connecting to SQL Server 2017 running inside the container using SQL Server Management Studio 18

 
As we can see, SQL Server is being executed on port 1433. Please specify the Server name “localhost,1433”, Login “sa”, and Password “Sample123$” to login into the SQL Server running inside the container.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 
Once we are successfully logged in, right mouse click on “Databases” -> “New Database …”. It will display a dialog, wherein you need to specify the “webapidemodb”. Also, observe that the database and log files are getting created inside “/var/opt/mssql/data” inside the container. Please refer to the image below.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 
Open a new query window (Right-click on “webapidemodb” -> New Query). From the SqlScripts folder execute only 1Create_Professors.sql, 2Create_Students.sql and 3Insert_Professors.sql file. It should create two tables and populate the Professors table.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 
Once we have successfully executed the 3 script files, we can perform the select * from both Professors and Students table. Please review the image below.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 
Let’s verify the database and log files exist inside the container. Please execute the “docker exec -it sqlserver2017withoutmount bash” command. It should take us inside the container. We can execute ls (listing) for directory listing. As we know the path of files is “/var/opt/mssql/data”, let's navigate and verify that our webapidemodb.mdf and webapidemodb_log.ldf exists.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 

Connecting to the database created in SQL Server 2017 running inside the container in Web API

 
Let’s use the database which we have created inside the Docker container in our Web API solution. Please open College.Services.sln solution inside Visual Studio 2017. Please open appsettings.json and modify the “CollegeDBConnectionString” inside “ConnectionStrings”."CollegeDBConnectionString": "Server=tcp:localhost,1433;Database=webapidemodb;User Id=sa;Password=Sample123$;".
 
Please refer to the image below.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 
Once that is done, please execute the Web API project and we should be able to see 3 professors' information inside the browser (in our case it is Chrome).
 
SQL Server 2017 Docker Container And Web API In .Net Core
 

Verifying, Deleting and re-creating SQL Server 2017 container to ensure that database (webapidemodb) we created is lost

 
Verify the SQL Server container exists (docker ps -a). Then stop and delete the container using docker stop ContainerId and docker rm ContainerId. Also, verify that the SQL Server container is not available (docker ps -a). Please refer to the image below.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 
Execute the command to create the SQL Server 2017 container without volume mount and verify that our webapidemodb files are lost.
 
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Sample123$" -p 1433:1433 --name sqlserver2017withoutmount -d mcr.microsoft.com/mssql/server:2017-latest
 
Please refer to the image below.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 
Let’s verify the database and log files inside the container are lost. Please execute the “docker exec -it sqlserver2017withoutmount bash” command. It should take us inside the container. We can execute ls for directory listing. As we know the path of files is “/var/opt/mssql/data”, let's navigate and verify that our webapidemodb.mdf and webapidemodb_log.ldf are lost.
 

Re-creating SQL Server 2017 container with volume mount to store the database and log files outside the container

 
With the above experiment we understand that if we create SQL Server container without volume mount and have the database and log files inside the container, they will be lost if we delete the container. Now let’s delete and re-create the container with volume mount.
Verify the SQL Server container exists (docker ps -a). Then stop and delete the container using “docker stop ContainerId and docker rm ContainerId”. Also, verify that the SQL Server container is not available (docker ps -a).
 
Let’s execute the below-mentioned command to create SQL Server 2017 container with volume mount. “C:\DockerVolumes\formssql” is the path from our local Laptop, and /var/opt/mssql/data the folder inside the container. That will create the database and log files outside the container inside (C:\DockerVolumes\formssql). Please refer to the image below.
 
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Sample123$" -p 1433:1433 --name sql1 -v C:\LordKrishna\DockerVolumes\formssql:/var/opt/mssql/data -d mcr.microsoft.com/mssql/server:2017-latest
 
SQL Server 2017 Docker Container And Web API In .Net Core
 
Let create the “webapidemodb” database and execute all the 4 scripts from the SqlScripts folder. Execute the Web API and we should see the Professor(s) and Student(s) information in the browser. Assuming we have created the same database name, the tables are as we did in our first exercise.
 
SQL Server 2017 Docker Container And Web API In .Net Core
 

Summary

 
Using the SQL Server 2017 container will help the developer to quickly set up, develop, and deliver.