Provisioning SQL ServerInstancesDocker
Provisioning SQL ServerInstancesDocker
We DBAs tend to make a meal out of provisioning SQL Server instances. We just aren’t used
to the idea of being able to get a SQL Server instance up and running without a lengthy setup
and the installation of all those prerequisites. However, our Ops friends, especially the Linux
people, just smile and shake their heads when they see us staring at the SQL Server
installation screens. They save a lot of time by using Docker. We can do the same thing, using
PowerShell to script the process.
You can run SQL Server in a Linux environment using a Linux Docker, using Docker
Enterprise Edition to run a supported version of SQL Server in Linux in containers. You can
also run SQL Server in Windows Docker on Windows 10 or Windows server 2016. It’s also
possible to run SQL Server in Docker on a Mac!
Docker Overview
So, what is Docker? Docker is an open-source platform, written in Go, which is a high-
performance language from Google. Go gives Docker extreme portability because the
compiled application doesn’t rely on external dependencies.
Before you start, create your machine, virtual or not, install Windows Server 2016 and then
follow the PowerShell code step by step in Admin mode (‘Run as Adminstrator’)
Setting up Docker
At the time of this writing, Docker have made two versions of Docker for Windows available.
For Windows 10, use the Docker Community Edition. Since this article is about using
Windows Server 2016, you will install the Docker Enterprise Edition for Windows Server if
you care to follow along.
Start up a PowerShell command window running as an administrator and run the following
commands to download and install the latest version of Docker:
If prompted to install any prerequisites such as NuGet, type in Y and restart once the
installation is complete.
Docker version
Then you can test out Docker by downloading a container that they have for that purpose
called hello-world:nanoserver.
Docker checks first to see whether the image is already there. If not, it downloads it and runs
it.
Checking the Microsoft Repository and Download SQL
Server
Now that Docker is successfully installed and running, you can begin creating a SQL Server
container. Check the Microsoft repository at Docker:
Docker images
Spin up a SQL Server Instance
Once the image has been downloaded, it’s time to spin up the SQL Server instance. From an
elevated PowerShell session, you can use the docker run command to set the required
properties and run the instance. Here is an explanation of the parameters:
docker ps
Connecting to the SQL Server
To connect to SQL Server from inside the VM, you need to know the IP of the container.
Inspect <container name> will return a lot of information. You need to find the correct
node in the JSON document and save it in a variable
Now you can connect to SQL Server using SSMS from within the virtual machine using the
container’s internal IP. Substitute your own IP and use sa with the password you provided
when spinning up the instance. Of course, you will need SSMS installed on the VM.
And we have confirmed that it’s working!
If you are working in an Azure VM, you must also add the port to the Network Interface in
the Azure Portal on the Networking page of your VM. Set the port up as a Custom port in
both the Inbound and Outbound security rules.
You will also need to determine the external IP of the VM. If running in Azure, you can find
the IP on the Networking page of the VM. If you are following along using another method,
just determine the IP you use to connect to your VM.
Now you can connect to SQL Server, passing the container port to differentiate between the
containers. In this example, 13.86.80.154,14333.
Managing the Container
Whenever you restart the VM, you will find that the containers are no longer running. If you
run the docker ps command used earlier to view the running containers, you’ll see this after
a restart.
Docker ps
The containers are not gone, they are just not running. You can add the -a switch to see all
containers, regardless of status.
Docker ps -a
To start a container, use the docker container start command.
You can also stop, restart, remove, and more with the Docker container command. For
example, this script stops the SQL Server instance and then removes it.
The ability to quickly create and tear down instances is probably the biggest advantage of
using Docker to host SQL Server.
If you removed sql01 by following the last example, run this command to create it once more
so you can follow along with the rest of the article.
If your container must run automatically after a restart of the host, you can add the restart
flag to the command.
Managing the SQL Server Instance
You will find that managing the SQL Server instance is similar to managing a traditional
instance, but since the container runs in its own space, there are a few differences. Since the
container lives in its own space, it cannot see outside that space. For example, it cannot
directly backup to a folder outside the container. To demonstrate, first list the directory
contents of the C:\ drive on the VM.
Dir c:\
You will probably see a different set of folders than what I see on my VM. Now, connect a
PowerShell session directly to the container by running this command:
The PowerShell window will change, and you will see a new session running from inside the
container. Run the ls or dir command to see the folder contents.
The folder list is very different than that of the VM since you are viewing inside the
container.
1. On the host machine, create a C:\Temp folder if one does not exist.
2. Download the WideWorldImporters_Full.bak file to the C:\Temp folder.
3. Make sure you have a PowerShell session running that is connected to the container.
4. Create a C:\Temp\Backup folder in the container
Md Temp
CD Temp
MD Backup
5. You can then switch back to the host in the PowerShell Session using Exit.
Exit
6. Now that the directory is created, you can use docker to copy the .bak file to the backup
folder in the container using the docker cp command. Note that the directory and file
names are case sensitive.
Dir temp\backup
Restoring the Database
Now that the file is in a place that the SQL Server instance can see it, you should connect
with SSMS and do a restore as usual. If you wish to connect from within the VM, you will
probably need to determine the IP address again using the script from the earlier section
within a PowerShell session running in the host:
2. Find the location of the new volume. By default, volumes are created at the hidden
location C:\ProgramData\docker\volumes
3. Now you will create a new container called sql02 that will be able to use the new volume.
The command uses the v switch to map the volume on the host to the container.
copy c:\temp\WideWorldImporters-Full.bak
C:\ProgramData\docker\volumes\sqlbackups
5. Connect to the new container with PowerShell.
Dir backups
Now there is a folder that both the host and container can see. The SQL Server instance
can backup directly to the backups folder. If you actually wanted to use one of these
mounted volumes for the database files (mdf, ldf, ndf) you could as well.
Conclusion
When Microsoft announced support for SQL Server on Linux and Docker, it was obvious that
the world of the DBA was changing. This was smart on Microsoft’s part as it can possibly get
SQL Server in shops that wouldn’t have considered it in the past.
This article showed how to get Docker running on a server and how to create a container
running SQL Server. It then discussed how to work with the container and with the instance.
Be sure to take a look at the Docker documentation to get more ideas on how to better
manage SQL Server running on Docker.
I would like to thank my good friend Anderson Alves de Souza for the initial research.
Leave a Comment
Simple Talk Back to top