How to Install SQL Server on Docker

As you may know, SQL Server can be installed on Linux distributions, one of them is a Docker Container, to follow this little tutorial (If you are using a Windows system) you should have installed WSL2 (Windows Subsystem for Linux 2) installed, here you have a little something to get you started.

Let’s Start Installing Docker

Contents

Installing Docker

Go to this LINK. Download the Docker of your choice

I am using a Mac, what should I do now?

First of all, download Docker.dmg, open a Terminal in the location of the Docker.dmg (If your Mac has an intel chip click HERE and if it has an apple chip click HERE) and write the following commands:

 # With this command you attach the Docker file to Mac 
 sudo hdiutil attach Docker.dmg
 # Now you should install it
 sudo /Volumes/Docker/Docker.app/Contents/MacOS/install
 # Finally, detach the volume
 sudo hdiutil detach /Volumes/Docker

That’s all, easy right? Now you should have docker installed on your Mac.

Using Windows, this is what you should do

You can start by downloading Docker Desktop Install.exe and execute it, if you have installed WSL2 as I told you before the installation process is very straightforward, you shouldn’t have any problem here.

What about Linux? what should I do now?

Well, this will depend on the Linux distro, Docker supports Ubuntu, Debian, Fedora and Arch (Currently as experimental) installations, and come with a package for each one of them.

Also, Docker in Linux needs KVM virtualization support, run the following commands in order to activate it:
modprobe kvm
#If you have an intel chip
modprobe kvm_intel
#Else, if you have an AMD chip  
modprobe kvm_amd    
sudo usermod -aG kvm $USER #Add your USER to the kvm group to access the kvm device

If you still have problems running Docker after this, you should read Docker Official Documentation on Linux Installation in order to make some troubleshooting.

Did all this, have Docker running, now lets Install Microsoft SQL Server in a Docker Container!

Lucky for us, Microsoft already took care of ourselves and make different images for SQL Server.

On Windows open a Powershell and run the following code:

docker pull mcr.microsoft.com/mssql/server:2022-latest

Otherwise, on Linux or Mac, kind of the same thing but with sudo ahead:

sudo docker pull mcr.microsoft.com/mssql/server:2022-latest

In case you want to use other SQL Server versions run one of the followings instead:

docker pull mcr.microsoft.com/mssql/server:2019-latest # SQL Server 2019
docker pull mcr.microsoft.com/mssql/server:2017-latest # SQL Server 2017

Ready! We have an SQL Server Image installing in our Docker Container, If you go to your docker images you should see it.

Configuring our SQL Server Installation

Now that we have our SQL Server Installation, Microsoft asks us to run it Accepting EULA before, so we should run this command:

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=[put your new password here]" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2022-latest # Take note this is only for server:2022-latest, in case you install other SQL server, you should change the last part of this line

If everything go Smoothly it will give you a hash line and in your docker Images you will see a “IN USE” label on your MCR image.

Connecting to our Docker container

We already have our Docker container with SQL Server up and running, one way to connect to run SQL commands is through sqlcmd inside the container, to connect to it use the following commands:

PS> docker ps -a # We will use this command to find out our container name
CONTAINER ID   IMAGE                                        COMMAND                  CREATED          STATUS                        PORTS                                         NAMES
463f5132aa17   mcr.microsoft.com/mssql/server:2022-latest   "/opt/mssql/bin/perm…"   12 minutes ago   Up 12 minutes                 0.0.0.0:1433->1433/tcp                        vigorous_rosalind
1c16dedd96f6   recordviewer_nginx                           "/docker-entrypoint.…"   2 months ago     Exited (255) 44 minutes ago   0.0.0.0:8080->80/tcp, 0.0.0.0:8443->443/tcp   recordviewer_nginx_1
69740c6d6ef1   recordviewer_web                             "bash -c 'node app.j…"   2 months ago     Exited (255) 44 minutes ago                                                 recordviewer_web_1

PS> docker exec -it vigorous_rosalind "bash" # In my case the Container was called vigorous_rosalind so let's run bash on it

# if all goes well, you are inside the SQL container right now

mssql@463f5132aa17:/$  /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "[PUT YOUR PASSWORD HERE]"
1> CREATE DAtABASE TestDB;
2> SELECT Name from sys.databases;
3> GO
Name
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
TestDB
1> QUIT
mssql@463f5132aa17:/$ exit

Perfect, we could run a command inside our SQL Server, everything is running smoothly.

Connect Using SQL Server Management Studio (SSMS)

If you are in Windows and followed our tutorial here you already have SSMS installed on your PC, in case you are on Linux or Mac consider using Azure Data Studio that comes with tools to manage an SQL server running on Linux

On SSMS, We should go to File > Connect Object Explorer…

On Authentication > Select SQL Server Authentication

On Server Name > [IP Address (If you are using the same machine, would be 127.0.0.1],[PORT (If you follow this tutorial step by step, would be 1433, you can change it when you run your Docker container)] (In my case: 127.0.0.1,1433)

Login: User (SA) as Default, to add new users we will make another tutorial, for now let’s start with this.

Password: The password you define when you create your Docker Container

Now you have your Docker Cointainer in your SSMS!

Conclusion

We have installed docker, clone an Image of a SQL Server, Dockerize it and connect to it. You could use this to make an staging development or to make a test environment.

Now you can Dockerize your SQL Servers! Hope this tutorial was useful!

Leave a Reply