Microsoft has officially supported running SQL Server in a container since SQL Server 2017. Today, most support is for SQL Server on Linux containers, and Microsoft only supports SQL Server 2022 on Linux containers for production workloads. In this article, I’ll show you how to configure SQL Server Docker containers on Linux.
Over the past several years, containerization platforms like Docker have emerged as a popular way to run various applications. Containers are essentially like virtualization at the application level. Unlike a virtual machine that provides virtualization at the hardware level, containers provide virtualization at the application level, and all containers that run on a given system share the same underlying hardware and operating system. This makes containers a much lighter-weight implementation than a VM.
One of the big advantages of running an application like SQL Server in a container is that no installation process is required. A SQL Server database instance can be up and running as soon as the container starts. Another big advantage is the container image is always the same whenever the container starts. Applications like SQL Server persist data outside of the container by using external volume storage.
For SQL Server 2022, SQL Server deployments in Windows containers are not supported. You can create your own custom SQL Server Windows containers but they should be restricted to development and testing.
Ubuntu is one of the most popular platforms for managing Docker and Kubernetes containers. Ubuntu does not come with Docker preinstalled, but installing Docker on Ubuntu is simply a matter of running a few commands in the Terminal window.
sudo apt update
sudo apt install docker.io -y
sudo snap install docker
This completes the installation for the Docker engine on Ubuntu. You can verify the installation simply by running the following docker command:
docker –version
You can see the results of this installation process in the figure below.
After Docker has been installed on Ubuntu, you’re ready to go ahead and pull down a SQL Server image. The following command shows how to use Docker to pull the SQL Server 2022 (16.x) Linux container image from the Microsoft Container Registry.
sudo docker pull mcr.microsoft.com/mssql/server:2022-latest
This command pulls the latest SQL Server 2022 (16.x) Linux container image because it’s using the latest tag. However, multiple SQL Server images are available to use on the Docker hub. Each SQL Server Docker image has a fully installed and ready-to-run instance of SQL Server.
To run the Linux container image with Docker, you can use the following command:
sudo docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD= TempPa55wd#" \ -p 1433:1433 --name sql2022 --hostname sql2022 \ -d mcr.microsoft.com/mssql/server:2022-latest
Let me explain what this command does in detail
This command runs the Developer edition of SQL Server 2022. To specify a different edition you need to use the ‘MSSQL_PID environment’ variable.
After the Docker container has been started you can use the following command to list the active containers:
sudo docker ps -a
You can see the commands to pull, run and list the SQL Server 2022 Docker container in the following figure.
You might notice all these examples are using the ‘sudo’ (Linux elevated root or Super User command) to run Docker commands. If you don’t want to use sudo to run Docker, you can configure a Docker group and add users to that group.
You can connect to the SQL Server instance running in a container exactly like connecting to any other SQL Server instance. However, you have the option of connecting from either inside the container or outside of the container.
All of the SQL Server Docker images have SQL Server command line tools (mssql-tools) installed, so you could start an interactive session using docker exec to run bash and then connect using command line tools. Alternatively, you can use regular SQL Server Management tools like SQL Server Management Studio (SSMS) and Azure Data Studio (ADS) to connect from outside the SQL Server container.
In order to connect to an SQL Server instance running in a Docker container with an external client tool such as SSMS, you need to know the container’s IP address. Every container will have a unique IP address much like a virtual machine would. You either need to register the container’s IP address and host name in your DNS, or you need to connect using the container’s raw IP address. Just using the IP address is generally simpler.
You can retrieve the IP address of a container using a command like the following.
sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ ab 172.17.0.2
Here, the docker inspect command is used in conjunction with the format switch to retrieve just the IP address of the container specified.
After getting the IP address of the container, you can use it to connect your SQL Server client tools to the container. In the following figure, you can see that we’re connecting Azure Data Studio to the running SQL Server 2022 Docker container.
Here, I supplied the container’s IP address and the SQL Server port number followed by the SA user name and password that I supplied earlier as a part of the docker run command.
At this point, you can go ahead and use Azure Data Studio to navigate through databases, restore databases, and run queries exactly like any other SQL Server instance.
In this tutorial, you saw how to install, run and connect to a SQL Server Docker container on Ubuntu Linux. Running SQL Server from a container has several advantages over traditional VMs or physical hosts.
Thank you for reading and please let me know in the comments if you have any questions about configuring SQL Server Docker containers on Linux.