Setting up SQL Server on Linux with Docker
How I spin up Microsoft SQL Server on Ubuntu using Docker, with sqlcmd for control. No native install, no VM.
بسم الله الرحمن الرحيم
Docker is the fastest way to get a Microsoft SQL Server instance running on a Linux box without dealing with a native install. I've been using this setup for local dev and for spinning up disposable databases when I'm prototyping. This is what I run.
Why Docker for SQL Server
You get an isolated instance with a single command, and you can rip it down and redo it just as fast when you break something. Compared to spinning up a full VM, it also uses a fraction of the memory. For local dev, that's the whole game.
There's a second reason worth mentioning: version isolation. If you're working across projects that need different SQL Server versions, or you want to test against a newer version without touching your default install, Docker containers let you run several side by side, each on a different port.
What you need
A Linux box. I use Ubuntu.
At least 2 GB of free RAM (SQL Server's floor).
An internet connection to pull the image (about 1.5 GB).
Docker Engine installed.
Install Docker
The official Docker guide is the source of truth if you're on a distro I'm not covering. Short version for Ubuntu:
# Add Docker's official GPG key
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get updateInstall the packages:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginVerify the install:
sudo docker run hello-worldIf Docker prints the "Hello from Docker!" message and exits cleanly, you're good.
Make sure the daemon is running:
sudo systemctl status docker
# start it if it isn't
sudo systemctl start dockerPull and run SQL Server
Pull the SQL Server 2022 image:
sudo docker pull mcr.microsoft.com/mssql/server:2022-latestRun the container. Replace YourStrong@Passw0rd with a real password (SQL Server's SA password policy requires at least 8 characters with a mix of uppercase, lowercase, digits, and symbols):
sudo docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=YourStrong@Passw0rd" \
-p 1433:1433 --name sql_server_container -d mcr.microsoft.com/mssql/server:2022-latestBreaking that down:
-e "ACCEPT_EULA=Y"— you need to accept Microsoft's EULA to run the container. Non-negotiable.-e "MSSQL_SA_PASSWORD=..."— the SA (system administrator) password.-p 1433:1433— expose the SQL Server port on the host so you can connect from outside the container.--name sql_server_container— a friendly name so subsequent commands don't need the container ID.-d— detached, runs in the background.
Check it's running:
sudo docker ps -aIf the status column shows Up for the container, you're in business. If it shows Exited, run docker logs sql_server_container to see what happened. The most common failure is an SA password that doesn't meet the complexity policy.
Install sqlcmd
sqlcmd is Microsoft's command-line client for talking to the instance. You can install it on the host so you don't have to shell into the container every time.
On Ubuntu 20.04:
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/prod.list)"
sudo apt-get update
sudo apt-get install sqlcmdOn newer Ubuntu (22.04+), swap the 20.04 in the URL for your version.
Connect and test
You can either shell into the container:
sudo docker exec -it sql_server_container "bash"...and then run sqlcmd from inside. Or you can just use the host's sqlcmd and connect over the exposed port. Either way, here's a test that creates a small stored procedure and calls it:
/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "<PASSWORD>" -C -Q "CREATE PROCEDURE GetVersion AS BEGIN SELECT @@VERSION END"
/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "<PASSWORD>" -C -Q "GetVersion"The -C flag tells sqlcmd to trust the server's self-signed certificate, which is what you'll be dealing with on a local dev container. In production you'd have a properly-issued cert and drop the -C.
If the second command returns the SQL Server version string, everything is wired up. You have a running SQL Server instance answering queries.
Where to go from here
A few things worth knowing once you're up:
Persisting data across container restarts: mount a volume with
-v sqlvolume:/var/opt/mssqlon thedocker run. Without it, dropping the container drops all your data.Connecting from your dev tools: SSMS, Azure Data Studio, and DBeaver all connect the same way.
localhost,1433as the server,saas the user, your SA password, and either "trust server certificate" or "encryption: optional" depending on the client.Stopping and starting:
docker stop sql_server_containerto pause,docker start sql_server_containerto bring it back. State persists across stop/start. It only disappears if youdocker rmthe container.
Salaam.