SQL Server 2017 is the first SQL Server version that is available on Windows, Linux, and in Docker containers. In this blog post, I will show you how to create your first SQL Server Docker container in a few very simple steps.
What is Docker?
Docker is an open-source container platform provider written in GO programming language. It’s available for Linux, Windows, and MacOS. In addition, it also works in Azure and AWS environments. Containers allow developers to package up an application with all of the parts it needs, such as libraries and other dependencies, and deliver it all out as one package. It’s something similar to Virtual Machines but without the guest operating system. They’re isolated, but they share Host OS. That makes them smaller and more lightweight.
Docker installation
Currently, for Windows users, Docker is available in two editions. Docker Community Edition for Windows (CE) is a free solution designed for Windows 10 users. Docker Enterprise Edition for Windows Server (EE) is dedicated for Windows Server 2016 users. There is also third (legacy) solution – Docker Toolbox with support for Windows 7 and 8. However, this edition has some additional requirements and limitations, for example, it supports only Linux containers.
Docker CE
To install Docker CE you need to follow two very simple steps:
- Download Docker CE installer from Docker Store.
- Double-click Docker for Windows Installer.exe and follow the install wizard to accept the license, authorize the installer, and proceed with the install.
Docker EE
Installation of Docker EE is more automated and can be performed using PowerShell:
- Open PowerShell command prompt and run following command to install Docker:
Install-Module DockerProvider -Force
Install-Package Docker -ProviderName DockerProvider -Force
- If prompted to install any prerequisites such as NuGet, type in “Y”.
- Run the following command to check if a reboot is required:
(Install-WindowsFeature Containers).RestartNeeded
- If the result is Yes, restart your computer with this command:
Restart-Computer
Docker Toolbox
Installation of Docker Toolbox is quite similar to the installation of Docker CE.
- Download Docker Toolbox installer.
- Install Docker Toolbox by double-clicking the installer.
The installer installs following software on your machine:
- Docker Client for Windows
- Docker Toolbox management tool and ISO
- Oracle VM VirtualBox
- Git MSYS-git UNIX tools
Installation validation
When Docker is already installed and running you may want to check if it works properly. For Docker CE and EE, start PowerShell command prompt. For Docker Toolbox, open Docker Toolbox Terminal (Docker QuickStart Terminal). Then run the following command to check your installed version:
docker --version
Next step is to check if Docker can pull and run containers:
docker run hello-world
Creating SQL Server Docker container
Currently, Microsoft provides three SQL Server Docker containers:
You may look for them on Docker Hub page or you also may run the following command to list available images:
docker search microsoft
Pull needed image
The first thing to start your container is to pull the image from the repository. Depending on what image you want to use you need to execute one of the following commands:
docker pull microsoft/mssql-server-linux
docker pull microsoft/mssql-server-windows-developer
docker pull microsoft/mssql-server-windows-express
Important: Docker Toolbox supports only Linux containers. Docker CE and EE support Linux and windows containers, however, to pull and run docker image without any issues you need to ensure that your Docker installation is running in the proper mode. You may do it by right-clicking on the Docker whale icon in your tray .
I decided to download SQL Server Developer on Windows image.
To check what images are downloaded run the following command:
docker images
Run new container
Finally, you’re ready to run your container. To do it you need to execute docker run
command and provide few mandatory parameters.
--name
– provide a name for your container
-d
– run container in detached mode (in the background)
-p
– publish a container᾿s port or a range of ports to the host. For example -p 14333:1433
maps port 14333 on the host to port 1433 in the container.
-e
– create environment variables under the container execution runtime
sa_password
(to assign the SQL Server SA password)
ACCEPT_EULA
(to accept end-user license agreement)
Example:
docker run --name <CONATINER_NAME> -d -p 14333:1433 -e sa_password=<SA_PASSWORD> -e ACCEPT_EULA=Y microsoft/mssql-server-windows-developer
Here you can find more details about docker run command.
How to connect to your new SQL Server?
The last thing is to connect to new SQL Server instance. To do this open SQL Server Management Studio and as Server name provide localhost with the port you specified for mapping. Then, choose SQL Server authentication and log in using sa account (with the password you specified during container setup).
Now you can run any queries you want.
-Marek
Share it: