Setup SQL Server

Setup SQL Server

How to Install SQL Server Using Docker on Linux

If you're looking to run Microsoft SQL Server on a Linux machine without the hassle of a full installation, Docker is a fantastic solution. Containers allow you to quickly spin up an isolated SQL Server instance with minimal setup. In this guide, I’ll walk you through the process of installing SQL Server using Docker on a Linux system, step by step.

Why Use Docker for SQL Server?

Docker simplifies deployment by packaging SQL Server into a container that runs consistently across different environments. Whether you're a developer testing an app or a sysadmin prototyping a database, this approach saves time and reduces compatibility headaches. Plus, it’s lightweight compared to a full virtual machine.

Prerequisites

Before we dive in, ensure you have the following:

  • A Linux distribution installed (I use Ubuntu for my examples).
  • At least 2 GB of free RAM (SQL Server’s minimum requirement).
  • An internet connection to pull the SQL Server image.

Step-by-Step Installation

1. Install Docker Engine (if you don't already have it installed)

  • You can check out the full official Docker installation guide, but here is a quick rundown for Ubuntu systems
  • Set up Docker's apt repository by running the following in terminal
# 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 update
  • Install the latest Docker packages
 sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • Verify that the installation is successful by running the hello-world image:
sudo docker run hello-world

2. Next, ensure the Docker daemon is running:

#check status 
sudo systemctl status docker

#if not running, start Docker
sudo systemctl start docker

2. Pull the SQL Server (16.x) Linux container image from the Microsoft Container Registry.

sudo docker pull mcr.microsoft.com/mssql/server:2022-latest

3. Run the SQL Server Container

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-latest

4. Verify success by checking the list of running Docker containers:

sudo docker ps -a

5. Install sqlcmd to be able to control the SQL Server Docker instance:

sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/prod.list)"
sudo apt-get update
sudo apt-get install sqlcmd

6. Start an interactive bash shell inside your running container to connect to SQL Server via sqlcmd:

sudo docker exec -it sql1 "bash"

Congratulations!

By this point you should have successfully been able to install and setup Docker, SQL Server, and have a running interactive bash shell that will allow you to control your server via queries.

We can test it by creating a stored procedure by running the following

/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"