In October 2017 Microsoft announced the general availability of SQL Server 2017 for Windows, Linux and Docker. And since I started to play around with Docker I thought it's a good idea to setup my next development environment for SQL Server on Docker?
- Run Docker Windows Container On Linux
- Run Docker Container Windows
- Run Docker Container Windows 8.1
- Run Docker Container On Windows 10
- Run Docker Container As Windows Service
- Run Docker Container Windows 7
- Run Docker Windows Container On Mac
Why? Because Docker provides the ability to package and run an application in a loosely isolated environment called a container. And a Docker container is much more lightweight than a Virtual Machine, because it doesn't need an extra Operating System, it runs directly within the host machine's kernel.
If you want to learn more about Docker itself, please have a look here: https://docs.docker.com/engine/docker-overview/
Let's get started
Build and Run Your First Windows Server Container (Blog Post) gives a quick tour of how to build and run native Docker Windows containers on Windows 10 and Windows Server 2016 evaluation releases. Getting Started with Windows Containers (Lab) shows you how to use the MusicStore application with Windows containers. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. Container images become containers at runtime and in the case of Docker containers - images become containers when they run on Docker Engine.
Since I run Windows 10 on my developer machine it need to setup SQL Server in a Windows Docker container and I figured out, that there some pitfalls that I came along. So I though it's maybe benefitial to write a Step by step setup guide.
Step 1: Download and Setup Docker for Windows
To get started we need to download and install Docker first on our machine. You can find the Windows setup here: https://www.docker.com/docker-windows
And there is also a very nice setup guide you can find here: https://docs.docker.com/docker-for-windows/install/. After your setup I would recommend to test if everything works fine. Just follow these examples and run your 'Hello World' on Docker.
Additionally I would recommend to install Kitematic, a simple and powerful graphical user interface to manage your Docker environment.
Step 2: Setup Docker for Windows containers
Run Docker Windows Container On Linux
After installing Docker on your machine Docker will start automatically in the background. As per default, Docker runs with Linux containers and you have to switch to Windows containers first. This is very is, just right click on your Docker icon in the taskbar and select 'Switch to Windows containers'.
Step 3: Get your SQL Server container image
After we performed the basic setup steps, we now need to get the SQL Server docker image. There are two sources available to get preconfigured images:
- Docker Store: The store is for publishers, that want to sell and distribute docker content (more details).
- Docker Hub: The hub is a cloud-based image registry to link to publishers own code repositories (more details).
Microsoft made SQL Server available in the Store and in the Hub:
- SQL Server in Docker Store:
- Linux Developer Edition: https://store.docker.com/images/mssql-server-linux
- Windows Express Edition: https://store.docker.com/images/mssql-server-windows-express
- SQL Server in Docker Hub:
- Linux Edition: https://hub.docker.com/r/microsoft/mssql-server-linux/
- Windows Developer Edition: https://hub.docker.com/r/microsoft/mssql-server-windows-developer/
- Windows Express Edition: https://hub.docker.com/r/microsoft/mssql-server-windows-express/
I will continue with the SQL Server 2017 Developer Edition for Windows. In order to download the image run the following command in Powershell oder the Commandline.
This will download the latest version of SQL Server 2017. This step can take a while, depending on your internet connection.
Step 4: Start your SQL Server image
After your image download has been completed you can start your SQL Server image with the following command:
For example you can run it like this:
In this case, the image will have a friendly name 'sql1' and SQL Server will run on port 1433.
For more details please have a look here: https://hub.docker.com/r/microsoft/mssql-server-windows-developer/
Step 5: Test your connection with sqlcmd
In order to test if the image works fine, connect with sqlcmd and run a short sql query. To connect with sqlcmd you can run the following command in your command line:
In order to figure the IP address of your Docker image you can run:
For example
Within sqlcmd run a small SQL statement like:
Step 6: Create a SQL Server Login
Now that you can connect with your SQL Server I would recommend to create a login instead of using the SA account. You can do this also with sqlcmd:
Step 7: Connect with SQL Server Management Studio
When you can connect with sqlcmd you can of course also use SQL Server Management Studio to connect to your SQL instance. You can find the latest version of SQL Server Management Studio here: https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms
Start SSMS and then connect to SQL Server as you would also do it with a normal local machine:
Step 8: Download and install sample Database AdventureWorks
Very good now we are able to use our SQL Server environement as we are normally used to it. But it makes for fun, if we would have at least some data to play with. Let's get AdventureWorks and install it on our Docker image.
AdventureWorks is a sample database that you can download here.
In my case I downloaded the AdventureWorks Data Warehouse backup file.
Next we need to get the backup into our image. To do so you have to first stop your image and then run the following command:
In my case:
Start your image again, get your new IP-Address and connect with SQL Server Management Studio. Then restore your database from your backup file and you are good to go.
Step 9: Query some data and have fun
The last step is the easiest one. Just fire some SQL queries in your Management Studio.
Sources:
Applies to: SQL Server (all supported versions) - Linux
Note
The examples shown below use the docker.exe but most of these commands also work with Podman. It provides the CLI similar to Docker container Engine. You can read more about podman here.
In this quickstart, you use Docker to pull and run the SQL Server 2017 container image, mssql-server-linux. Then connect with sqlcmd to create your first database and run queries.
Tip
If you want to run SQL Server 2019 containers, see the SQL Server 2019 version of this article.
Note
Starting with SQL Server 2019 CU3, Ubuntu 18.04 is supported.
In this quickstart, you use Docker to pull and run the SQL Server 2019 container image, mssql-server. Then connect with sqlcmd to create your first database and run queries.
Tip
Run Docker Container Windows
This quickstart creates SQL Server 2019 containers. If you prefer to create SQL Server 2017 containers, see the SQL Server 2017 version of this article.
This image consists of SQL Server running on Linux based on Ubuntu 18.04. It can be used with the Docker Engine 1.8+ on Linux or on Docker for Mac/Windows. This quickstart specifically focuses on using the SQL Server on Linux image. The Windows image is not covered, but you can learn more about it on the mssql-server-windows-developer Docker Hub page.
Prerequisites
- Docker Engine 1.8+ on any supported Linux distribution or Docker for Mac/Windows. For more information, see Install Docker.
- Docker overlay2 storage driver. This is the default for most users. If you find that you are not using this storage provider and need to change, see the instructions and warnings in the docker documentation for configuring overlay2.
- Minimum of 2 GB of disk space.
- Minimum of 2 GB of RAM.
- System requirements for SQL Server on Linux.
Pull and run the 2017 container image
Before starting the following steps, make sure that you have selected your preferred shell (bash, PowerShell, or cmd) at the top of this article.
Pull the SQL Server 2017 Linux container image from Microsoft Container Registry.
Tip
If you want to run SQL Server 2019 containers, see the SQL Server 2019 version of this article.
The previous command pulls the latest SQL Server 2017 container image. If you want to pull a specific image, you add a colon and the tag name (for example,
mcr.microsoft.com/mssql/server:2017-GA-ubuntu
). To see all available images, see the mssql-server Docker hub page.For the bash commands in this article,
sudo
is used. On macOS,sudo
might not be required. On Linux, if you do not want to usesudo
to run Docker, you can configure a docker group and add users to that group. For more information, see Post-installation steps for Linux.To run the container image with Docker, you can use the following command from a bash shell (Linux/macOS) or elevated PowerShell command prompt.
Note
If you are using PowerShell Core, replace the double quotes with single quotes.
Note
The password should follow the SQL Server default password policy, otherwise the container can not setup SQL server and will stop working. By default, the password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols. You can examine the error log by executing the docker logs command.
By default, this creates a container with the Developer edition of SQL Server 2017. The process for running production editions in containers is slightly different. For more information, see Run production container images.
The following table provides a description of the parameters in the previous
docker run
example:Parameter Description -e 'ACCEPT_EULA=Y' Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image. -e 'SA_PASSWORD=' Specify your own strong password that is at least 8 characters and meets the SQL Server password requirements. Required setting for the SQL Server image. -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 sql1 Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you cannot reuse this same name. -h sql1 Used to explicitly set the container hostname, if you don't specify it, it defaults to the container ID which is a randomly generated system GUID. -d Run the container in the background (daemon) mcr.microsoft.com/mssql/server:2017-latest The SQL Server 2017 Linux container image. To view your Docker containers, use the
docker ps
command.You should see output similar to the following screenshot:
If the STATUS column shows a status of Up, then SQL Server is running in the container and listening on the port specified in the PORTS column. If the STATUS column for your SQL Server container shows Exited, see the Troubleshooting section of the configuration guide.
The -h
(host name) parameter as discussed above, changes the internal name of the container to a custom value. Rise of kingdoms download android. This is the name you'll see returned in the following Transact-SQL query:
Setting -h
and --name
to the same value is a good way to easily identify the target container.
- As a final step, change your SA password because the
SA_PASSWORD
is visible inps -eax
output and stored in the environment variable of the same name. See steps below.
Pull and run the 2019 container image
Run Docker Container Windows 8.1
Before starting the following steps, make sure that you have selected your preferred shell (bash, PowerShell, or cmd) at the top of this article.
Pull the SQL Server 2019 Linux container image from Microsoft Container Registry.
Note
If you are using PowerShell Core, replace the double quotes with single quotes.
Tip
This quickstart uses the SQL Server 2019 Docker image. If you want to run the SQL Server 2017 image, see the SQL Server 2017 version of this article.
The previous command pulls the SQL Server 2019 container image based on Ubuntu. To instead use container images based on RedHat, see Run RHEL-based container images. To see all available images, see the mssql-server-linux Docker hub page.
For the bash commands in this article,
sudo
is used. On macOS,sudo
might not be required. On Linux, if you do not want to usesudo
to run Docker, you can configure a docker group and add users to that group. For more information, see Post-installation steps for Linux.To run the container image with Docker, you can use the following command from a bash shell (Linux/macOS) or elevated PowerShell command prompt.
Note
The password should follow the SQL Server default password policy, otherwise the container can not setup SQL server and will stop working. By default, the password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols. You can examine the error log by executing the docker logs command.
By default, this creates a container with the Developer edition of SQL Server 2019.
The following table provides a description of the parameters in the previous
docker run
example:Parameter Description -e 'ACCEPT_EULA=Y' Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image. -e 'SA_PASSWORD=' Specify your own strong password that is at least 8 characters and meets the SQL Server password requirements. Required setting for the SQL Server image. -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 sql1 Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you cannot reuse this same name. -h sql1 Used to explicitly set the container hostname, if you don't specify it, it defaults to the container ID which is a randomly generated system GUID. mcr.microsoft.com/mssql/server:2019-latest The SQL Server 2019 Ubuntu Linux container image. To view your Docker containers, use the
docker ps
command.You should see output similar to the following screenshot:
If the STATUS column shows a status of Up, then SQL Server is running in the container and listening on the port specified in the PORTS column. If the STATUS column for your SQL Server container shows Exited, see Troubleshooting SQL Server Docker containers.
The -h
(host name) parameter as discussed above, changes the internal name of the container to a custom value. This changes the internal name of the container to a custom value. This is the name you'll see returned in the following Transact-SQL query:
Setting -h
and --name
to the same value is a good way to easily identify the target container.
- As a final step, change your SA password because the
SA_PASSWORD
is visible inps -eax
output and stored in the environment variable of the same name. See steps below.
Change the SA password
The SA account is a system administrator on the SQL Server instance that gets created during setup. After creating your SQL Server container, the SA_PASSWORD
environment variable you specified is discoverable by running echo $SA_PASSWORD
in the container. For security purposes, change your SA password.
Choose a strong password to use for the SA user.
Use
docker exec
to run sqlcmd to change the password using Transact-SQL. In the following example, replace the old password,, and the new password,
, with your own password values.
Connect to SQL Server
The following steps use the SQL Server command-line tool, sqlcmd, inside the container to connect to SQL Server.
Use the
docker exec -it
command to start an interactive bash shell inside your running container. In the following examplesql1
is name specified by the--name
parameter when you created the container.Once inside the container, connect locally with sqlcmd. Sqlcmd is not in the path by default, so you have to specify the full path.
Tip
You can omit the password on the command-line to be prompted to enter it.
If successful, you should get to a sqlcmd command prompt:
1>
.
Create and query data
The following sections walk you through using sqlcmd and Transact-SQL to create a new database, add data, and run a query.
Create a new database
Applies to: SQL Server (all supported versions) - Linux
Note
The examples shown below use the docker.exe but most of these commands also work with Podman. It provides the CLI similar to Docker container Engine. You can read more about podman here.
In this quickstart, you use Docker to pull and run the SQL Server 2017 container image, mssql-server-linux. Then connect with sqlcmd to create your first database and run queries.
Tip
If you want to run SQL Server 2019 containers, see the SQL Server 2019 version of this article.
Note
Starting with SQL Server 2019 CU3, Ubuntu 18.04 is supported.
In this quickstart, you use Docker to pull and run the SQL Server 2019 container image, mssql-server. Then connect with sqlcmd to create your first database and run queries.
Tip
Run Docker Container Windows
This quickstart creates SQL Server 2019 containers. If you prefer to create SQL Server 2017 containers, see the SQL Server 2017 version of this article.
This image consists of SQL Server running on Linux based on Ubuntu 18.04. It can be used with the Docker Engine 1.8+ on Linux or on Docker for Mac/Windows. This quickstart specifically focuses on using the SQL Server on Linux image. The Windows image is not covered, but you can learn more about it on the mssql-server-windows-developer Docker Hub page.
Prerequisites
- Docker Engine 1.8+ on any supported Linux distribution or Docker for Mac/Windows. For more information, see Install Docker.
- Docker overlay2 storage driver. This is the default for most users. If you find that you are not using this storage provider and need to change, see the instructions and warnings in the docker documentation for configuring overlay2.
- Minimum of 2 GB of disk space.
- Minimum of 2 GB of RAM.
- System requirements for SQL Server on Linux.
Pull and run the 2017 container image
Before starting the following steps, make sure that you have selected your preferred shell (bash, PowerShell, or cmd) at the top of this article.
Pull the SQL Server 2017 Linux container image from Microsoft Container Registry.
Tip
If you want to run SQL Server 2019 containers, see the SQL Server 2019 version of this article.
The previous command pulls the latest SQL Server 2017 container image. If you want to pull a specific image, you add a colon and the tag name (for example,
mcr.microsoft.com/mssql/server:2017-GA-ubuntu
). To see all available images, see the mssql-server Docker hub page.For the bash commands in this article,
sudo
is used. On macOS,sudo
might not be required. On Linux, if you do not want to usesudo
to run Docker, you can configure a docker group and add users to that group. For more information, see Post-installation steps for Linux.To run the container image with Docker, you can use the following command from a bash shell (Linux/macOS) or elevated PowerShell command prompt.
Note
If you are using PowerShell Core, replace the double quotes with single quotes.
Note
The password should follow the SQL Server default password policy, otherwise the container can not setup SQL server and will stop working. By default, the password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols. You can examine the error log by executing the docker logs command.
By default, this creates a container with the Developer edition of SQL Server 2017. The process for running production editions in containers is slightly different. For more information, see Run production container images.
The following table provides a description of the parameters in the previous
docker run
example:Parameter Description -e 'ACCEPT_EULA=Y' Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image. -e 'SA_PASSWORD=' Specify your own strong password that is at least 8 characters and meets the SQL Server password requirements. Required setting for the SQL Server image. -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 sql1 Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you cannot reuse this same name. -h sql1 Used to explicitly set the container hostname, if you don't specify it, it defaults to the container ID which is a randomly generated system GUID. -d Run the container in the background (daemon) mcr.microsoft.com/mssql/server:2017-latest The SQL Server 2017 Linux container image. To view your Docker containers, use the
docker ps
command.You should see output similar to the following screenshot:
If the STATUS column shows a status of Up, then SQL Server is running in the container and listening on the port specified in the PORTS column. If the STATUS column for your SQL Server container shows Exited, see the Troubleshooting section of the configuration guide.
The -h
(host name) parameter as discussed above, changes the internal name of the container to a custom value. Rise of kingdoms download android. This is the name you'll see returned in the following Transact-SQL query:
Setting -h
and --name
to the same value is a good way to easily identify the target container.
- As a final step, change your SA password because the
SA_PASSWORD
is visible inps -eax
output and stored in the environment variable of the same name. See steps below.
Pull and run the 2019 container image
Run Docker Container Windows 8.1
Before starting the following steps, make sure that you have selected your preferred shell (bash, PowerShell, or cmd) at the top of this article.
Pull the SQL Server 2019 Linux container image from Microsoft Container Registry.
Note
If you are using PowerShell Core, replace the double quotes with single quotes.
Tip
This quickstart uses the SQL Server 2019 Docker image. If you want to run the SQL Server 2017 image, see the SQL Server 2017 version of this article.
The previous command pulls the SQL Server 2019 container image based on Ubuntu. To instead use container images based on RedHat, see Run RHEL-based container images. To see all available images, see the mssql-server-linux Docker hub page.
For the bash commands in this article,
sudo
is used. On macOS,sudo
might not be required. On Linux, if you do not want to usesudo
to run Docker, you can configure a docker group and add users to that group. For more information, see Post-installation steps for Linux.To run the container image with Docker, you can use the following command from a bash shell (Linux/macOS) or elevated PowerShell command prompt.
Note
The password should follow the SQL Server default password policy, otherwise the container can not setup SQL server and will stop working. By default, the password must be at least 8 characters long and contain characters from three of the following four sets: Uppercase letters, Lowercase letters, Base 10 digits, and Symbols. You can examine the error log by executing the docker logs command.
By default, this creates a container with the Developer edition of SQL Server 2019.
The following table provides a description of the parameters in the previous
docker run
example:Parameter Description -e 'ACCEPT_EULA=Y' Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image. -e 'SA_PASSWORD=' Specify your own strong password that is at least 8 characters and meets the SQL Server password requirements. Required setting for the SQL Server image. -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 sql1 Specify a custom name for the container rather than a randomly generated one. If you run more than one container, you cannot reuse this same name. -h sql1 Used to explicitly set the container hostname, if you don't specify it, it defaults to the container ID which is a randomly generated system GUID. mcr.microsoft.com/mssql/server:2019-latest The SQL Server 2019 Ubuntu Linux container image. To view your Docker containers, use the
docker ps
command.You should see output similar to the following screenshot:
If the STATUS column shows a status of Up, then SQL Server is running in the container and listening on the port specified in the PORTS column. If the STATUS column for your SQL Server container shows Exited, see Troubleshooting SQL Server Docker containers.
The -h
(host name) parameter as discussed above, changes the internal name of the container to a custom value. This changes the internal name of the container to a custom value. This is the name you'll see returned in the following Transact-SQL query:
Setting -h
and --name
to the same value is a good way to easily identify the target container.
- As a final step, change your SA password because the
SA_PASSWORD
is visible inps -eax
output and stored in the environment variable of the same name. See steps below.
Change the SA password
The SA account is a system administrator on the SQL Server instance that gets created during setup. After creating your SQL Server container, the SA_PASSWORD
environment variable you specified is discoverable by running echo $SA_PASSWORD
in the container. For security purposes, change your SA password.
Choose a strong password to use for the SA user.
Use
docker exec
to run sqlcmd to change the password using Transact-SQL. In the following example, replace the old password,, and the new password,
, with your own password values.
Connect to SQL Server
The following steps use the SQL Server command-line tool, sqlcmd, inside the container to connect to SQL Server.
Use the
docker exec -it
command to start an interactive bash shell inside your running container. In the following examplesql1
is name specified by the--name
parameter when you created the container.Once inside the container, connect locally with sqlcmd. Sqlcmd is not in the path by default, so you have to specify the full path.
Tip
You can omit the password on the command-line to be prompted to enter it.
If successful, you should get to a sqlcmd command prompt:
1>
.
Create and query data
The following sections walk you through using sqlcmd and Transact-SQL to create a new database, add data, and run a query.
Create a new database
The following steps create a new database named TestDB
.
From the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:
On the next line, write a query to return the name of all of the databases on your server:
The previous two commands were not executed immediately. Type
GO
on a new line to execute the previous commands:
Insert data
Next create a new table, Inventory
, and insert two new rows.
From the sqlcmd command prompt, switch context to the new
TestDB
database:Create new table named
Inventory
:Insert data into the new table:
Type
GO
to execute the previous commands:
Select data
Now, run a query to return data from the Inventory
table.
From the sqlcmd command prompt, enter a query that returns rows from the
Inventory
table where the quantity is greater than 152:Execute the command:
Exit the sqlcmd command prompt
To end your sqlcmd session, type
QUIT
:To exit the interactive command-prompt in your container, type
exit
. Your container continues to run after you exit the interactive bash shell.
Connect from outside the container
You can also connect to the SQL Server instance on your Docker machine from any external Linux, Windows, or macOS tool that supports SQL connections.
Run Docker Container On Windows 10
The following steps use sqlcmd outside of your container to connect to SQL Server running in the container. These steps assume that you already have the SQL Server command-line tools installed outside of your container. The same principles apply when using other tools, but the process of connecting is unique to each tool.
Find the IP address for the machine that hosts your container. On Linux, use ifconfig or ip addr. On Windows, use ipconfig.
For this example, install the sqlcmd tool on your client machine. For more information, see Install sqlcmd on Windows or Install sqlcmd on Linux.
Run sqlcmd specifying the IP address and the port mapped to port 1433 in your container. In this example, that is the same port, 1433, on the host machine. If you specified a different mapped port on the host machine, you would use it here. You will also need to open the appropriate inbound port on your firewall to allow the connection.
Run Transact-SQL commands. When finished, type
QUIT
.
Run Docker Container As Windows Service
Other common tools to connect to SQL Server include:
Remove your container
If you want to remove the SQL Server container used in this tutorial, run the following commands:
Warning
Stopping and removing a container permanently deletes any SQL Server data in the container. If you need to preserve your data, create and copy a backup file out of the container or use a container data persistence technique.
Docker demo
Run Docker Container Windows 7
After you have tried using the SQL Server container image for Docker, you might want to know how Docker is used to improve development and testing. The following video shows how Docker can be used in a continuous integration and deployment scenario.
Next steps
For a tutorial on how to restore database backup files into a container, see Restore a SQL Server database in a Linux Docker container. Explore other scenarios, such as running multiple containers, data persistence, and troubleshooting.
Run Docker Windows Container On Mac
Also, check out the mssql-docker GitHub repository for resources, feedback, and known issues.