Run SQL Server in Docker on Ubuntu

Run SQL Server in Docker on Ubuntu

In this article, we will guide you through the process of running SQL Server in a Docker container on an Ubuntu system. By the end of this tutorial, you will have a fully functional SQL Server instance running in a Docker container, and you will learn how to persist data and access the server from a .NET application.

Install docker

Update your package index

sudo apt-get update

Install Docker

sudo apt  install docker.io

Create a container

we are using the Microsft mcr.microsoft.com/mssql/server:2022-latest image from docker hub . Follow these steps to create the container

sudo docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=yourStrong@Password123!" \
   -p 1433:1433 --name sql1 --hostname sql1 \
   -d \
   mcr.microsoft.com/mssql/server:2022-latest

Persist your data with data volume containers

how to use data volumes to persist your database files even if the associated containers are deleted.

sudo docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=yourStrong@Password123!" \
   -p 1433:1433 --name sql1 --hostname sql1 \
   -v sqldata:/var/opt/mssql \
   -d \
   mcr.microsoft.com/mssql/server:2022-latest

Access your SQL server

you can access and run queries, and create a TestDB database to your SQL server (Running in Docker container) from GUI like SSMS or Azure Data Studio, by changing the server address to your IP address of the Ubuntu server.

Server=192.168.100.59,1433;Database=TestDB;Encrypt=false;User Id=sa;Password=yourStrong@Password123!;MultipleActiveResultSets=true"

I am doing this by going inside the container

 sudo docker exec -it sql1 "bash"

inside the container

/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "yourStrong@Password123!"

inside the SQL server

CREATE DATABASE TestDB;
SELECT Name from sys.databases;
GO

Create a Database

Create a Table

Accessing the SQL server Database in the .NET application

update your connecting string in appsettings.json to this, also replace the server address, and database name if you create with another name.

Did you find this article valuable?

Support Muhammad Usama by becoming a sponsor. Any amount is appreciated!