Getting Started with SQL Server in Docker Containers
It's a brave new world where Microsoft SQL Server can now run on non-Windows platforms. Join Tim Ford as he starts the journey beginning with downloading and installing Docker and querying SQL Server 2017 from (gasp!) a Mac!
We have entered a brave new world of computing when it comes to the Microsoft Data Platform. It has only been a few years when the running joke in the (then) SQL Server Microsoft MVP Community was essentially the point in time when SQL Server ran on Linux and Hell freezing over would be one and the same.
Pull out your fur lined swimsuit. We are running SQL Server on Linux – even on Macs.
This article is going to be a crash course on getting started with setting up the ability for you to do just that: run Microsoft SQL Server on Linux on your Mac (in my case a fairly old MacBook Pro.) In doing so I’ll also introduce you to installing Docker since we will be using the SQL Server 2017 image running inside a Docker container for just such a purpose.
Installing Docker on Your Mac
Installing Docker in surprisingly easy. You have your choice of channels for downloading the necessary installer: Stable or Edge. Both Stable and Edge installers include experimental features enabled by default and configurable in the Docker Daemon preferences for experimental mode. Since we are discussing getting started with this platform I strongly recommend that you disable experimental features for now and you definitely do so for any production situations. For the Stable channel proceed here; or for the experimental channel select here.
Either route brings up the standard drag and drop feature for the Mac OS. Drag the Docker logo to the Applications folder to install Docker.
Prerequisites for Docker for Mac
Docker for Mac requires OS X El Capitan 10.11 or a newer macOS release running on a 2010 or newer Mac device with Intel’s hardware support for MMU virtualization. While the app will run on 10.10.3 Yosemite, it will do so with only limited support. Please see What to know before you install for a full explanation and list of prerequisites.
At least 4GB of RAM but since we’ll be running SQL inside of a Docker container I’d recommend at least 8GB of RAM because one of the first configuration changes we’ll make to Docker before launching SQL is raising the Docker use of RAM to at least 4GB and we need RAM left over for the Mac OS and other applications to function properly.
VirtualBox prior to version 4.3.30 must NOT be installed due to incompatibility with Docker for Mac. Newer versions are perfectly acceptable however.
I also strongly suggest reading the README FIRST that comes with Docker for Mac before you continue.
Launching and Configuring Docker for Mac
After installing Docker for Mac you can launch it as you would any other application. When running you’ll see the Docker whale logo in the status bar at the top of your monitor.
At this point we need to make but one configuration change before we can move onto SQL Server 2017 in Docker: we need to adjust the RAM settings for Docker to meet minimums for SQL Server. Click on the Docker logo in the status bar, select Preferences, then Advanced. Slide the RAM setting to be at least 4GB as you see below then click Apply and Restart.
Installing the Microsoft SQL Server 2017 Docker Image
Just as there were pre-requisites for Docker for Mac there are additional requirements in addition to the RAM setting for the SQL Server Docker Image to run on any device:
Docker Engine 1.8+ on any supported Linux distribution or Docker for Mac/Windows.
Minimum of 4 GB of disk space
Minimum of 4 GB of RAM (though as mentioned I’d recommend at least 8GB.)
Believe it or not that was the hardest part to getting started with SQL Server 2017 in Docker. The final step is to pull the image from Docker and that all happens with a single command. Launch Terminal on your Mac OS device and enter the following command:
docker pull microsoft/mssql-server-linux
This will pull the image to your local Docker implementation. Once installed you’ll then connect to the instance of SQL as you would any other using SQLCmd inside of Terminal:
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=' -p 1433:1433 -d microsoft/mssql-server-linux
The command includes 4 defined parameters:
ACCEPT_EULA defines you’re acceptance of the End User License Agreement
SA_PASSWORD sets the sa password for the instance and must meet the standard minimum strength requirements for SQL Server
-p for your port mapping between the host environment and the Docker container. The first value represents the host whereas the second represents the container.
-d is the Docker container image you’re addressing. Unless specified otherwise this defaults to the most recent image
At this point you should now have a working SQL Server 2017 instance running in Docker. To get a list of your Docker containers you can run the following command in Terminal:
docker ps -a
What the output also gives you is a name for the container. In this case the important columns to look at are the CONTAINER ID, IMAGE, STATUS, and PORTS. The CONTAINER ID is important when querying your instance. It is for all practical matters your server name. The IMAGE confirms this is a SQL Server Docker image. If your STATUS shows as Up then that container is active. Finally the PORTS column confirms what I stated earlier about communication ports. If for some reason someone created the instance using a non-traditional port you’d see it here.
Final Step for This Article: Querying SQL in A Docker Container
The goal of this article is to get you up and running with SQL Server in Docker. We will go deeper in depth in future articles. (There is so much here to learn!) I want to at least give you a basic example of querying SQL inside Docker before I go. The syntax is quite similar to SQLCMD:
We once again use Terminal for this task on the MacBook. To connect you’ll need to specify the container id (identified through the process above), the path to SQLCMD in the container, and then the standard format in SQLCMD for server/instance, login, and password. You’ll then pass in the query text using the –Q parameter:
docker exec -it 0e1d4703f23d /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 1433!FOO!1433 -Q 'SELECT name FROM master.sys.databases;'
The results look like this for my brand new instance of SQL Server 2017:
name ---------master tempdb model msdb (4 rows affected)
I don’t normally say “it’s as simple as that” but in this case I’ll make an exception. Stay tuned here as I start my journey into SQL Server 2017, Linux, Docker, and (gasp) Mac OS. It’s sure to be a fun ride!
About the Author
You May Also Like