Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / containers / docker

Using SQL Server 2017 Docker Container for Local Web API development in .NET Core

5.00/5 (3 votes)
14 Jul 2019CPOL6 min read 12.2K  
In this article, we will be looking at how to use SQL Server 2017 Docker image for Local Web API development.

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

Pre-requisites

  1. Windows 10
  2. Visual Studio 2017
  3. Microsoft SQL Server Management Studio 18
  4. Docker for Windows (Linux containers)
  5. Client: Docker Engine - Community
    1. Version: 18.09.2
    2. API version: 1.39
  6. Server: Docker Engine - Community
    1. Version: 18.09.2
    2. API version: 1.39 (minimum version 1.12)

It Would Be Good to Have Prior Knowledge of

  1. Docker (https://www.docker.com/)
  2. EF Core 2.1
  3. Web API using .NET Core 2.1
  4. SQL Server Management Studio 18

Assumption

This article assumes that you have 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 Downloading 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

Image 1

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.

Image 2

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.

docker ps -a

Image 3

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.

Image 4

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:

Image 5

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.

Image 6

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:

Image 7

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.

docker exec -it sqlserver2017withoutmount bash
cd /var/opt/mssql/data
ls

Image 8

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:

Image 9

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).

Image 10

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:

docker ps -a
docker stop ContainerId
docker rm ContainerId
docker ps -a

Image 11

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:

Image 12

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.

docker exec -it sqlserver2017withoutmount bash
cd /var/opt/mssql/data
ls

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).

docker ps -a
docker stop ContainerId
docker rm ContainerId
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

Image 13

Let's 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 as we did in our first exercise.

Image 14

Summary

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

History

  • 14th July, 2019: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)