You can 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.
The 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 2019 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 2019 Linux container image from Microsoft Container Registry.
Bash
sudo docker pull mcr.microsoft.com/mssql/server:2019-latest
- To run the container image with Docker, you can use the following command from a bash shell (Linux/macOS) or elevated PowerShell command prompt.
Bash
sudo docker run -e “ACCEPT_EULA=Y” -e “SA_PASSWORD=<[email protected]>” \
-p 1433:1433 –name sql1 -h sql1 \
-d mcr.microsoft.com/mssql/server:2019-latest
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=<[email protected]\>" | 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.
Bash
sudo docker ps -a
You should see output similar to the following screenshot:

- If the
STATUS
column shows a status ofUp
, then SQL Server is running in the container and listening on the port specified in thePORTS
column. If theSTATUS
column for your SQL Server container showsExited
.
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:
SQL
SELECT @@SERVERNAME,
SERVERPROPERTY(‘ComputerNamePhysicalNetBIOS’),
SERVERPROPERTY(‘MachineName’),
SERVERPROPERTY(‘ServerName’)
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 in ps -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, <YourStrong!Passw0rd>, and the new password, <YourNewStrong!Passw0rd>, with your own password values.
Bash
sudo docker exec -it sql1 /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P “<[email protected]>” \
-Q ‘ALTER LOGIN SA WITH PASSWORD=”<[email protected]>”‘
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 example sql1 is name specified by the –name parameter when you created the container.
Bash
sudo docker exec -it sql1 “bash”
- Once inside the container, connect locally with sqlcmd. Sqlcmd is not in the path by default, so you have to specify the full path.
Bash
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P “<[email protected]>”
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:
SQL
CREATE DATABASE TestDB
- On the next line, write a query to return the name of all of the databases on your server:
SQL
SELECT Name from sys.Databases
- The previous two commands were not executed immediately. Type GO on a new line to execute the previous commands:
SQL
GO
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:
SQL
USE TestDB
- Create new table named Inventory:
SQL
CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
- Insert data into the new table:
SQL
INSERT INTO Inventory VALUES (1, ‘banana’, 150); INSERT INTO Inventory VALUES (2, ‘orange’, 154);
- Type GO to execute the previous commands:
SQL
GO
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:
SQL
SELECT * FROM Inventory WHERE quantity > 152;
- Execute the command:
SQL
GO
Exit the sqlcmd command prompt
- To end your
sqlcmd
session, type QUIT:
SQL
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.
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
orip addr
. On Windows, useipconfig
. - For this example, install the
sqlcmd
tool on your client machine. - 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.
Bash
sqlcmd -S <ip_address>,1433 -U SA -P “<[email protected]>”
- Run Transact-SQL commands. When finished, type QUIT.
Remove your container
If you want to remove the SQL Server container used in this tutorial, run the following commands:
Bash
sudo docker stop sql1
sudo docker rm sql1
Docker demo
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.