How to Run MSSQL Server in Windows Container
Slightly random introduction
I realise that Windows containers are not that popular. They are bulky, slow-starting buffalo, and almost always, there is a penguin which does the same job better.
Microsoft used to provide an official MSSQL Server 2019 Windows container image. However, the image has fallen out of fashion and out of support now, and what is left is an unmaintained docker file on GitHub.
Nowadays, if you want to run MSSQL in a container, you need to use MSSQL Linux image.
docker pull mcr.microsoft.com/mssql/server:2022-latest
docker pull mcr.microsoft.com/mssql/server:2019-latest
docker pull mcr.microsoft.com/mssql/server:2017-latest
Microsoft currently does not support running SQL Server on Windows Containers in a production scenario. But in some cases, like quick research, prototyping, proofs-of-concept, etc., having a Windows version of MS SQL Server is nice (especially when you run multiple Windows containers).
Build an MSSQL Server in Windows Container image locally
There is no official docker image to pull and if you publish one in a public repository, you may expect Microsoft to ask you to remove it.
But you can build it yourself. The solution is based on the code provided by Microsoft with some extra features, like building different SQL versions.
Clone the squirrelistic-blog repo and go to how_to_run_mssqlserver_in_windows_container directory.
git clone https://github.com/Squirrelistic/squirrelistic-blog.git
cd squirrelistic-blog\how_to_run_mssqlserver_in_windows_container
Build the Docker image. Note that the Docker will download ~1.5GB SQL Server Developer Edition installer ISO Image. If you plan to build docker images more than once, I recommend setting up a local file server to host the installers.
Choose which SQL Server (Developer Edition) version you need (2019 or 2022) and which base Windows Server image should be used (ltsc2019 or ltsc2022)
You won't be able to build ltsc2022 image on Windows 10 (only on Windows 11 or on Windows 2022 Server).
Build MSSQL Server 2022 with Windows 2019 Server base.
docker build -t mssqlserver:2022-ltsc2019 --build-arg SQL_VERSION=2022 --build-arg WIN_VERSION=ltsc2019 .
Build MSSQL Server 2022 with Windows 2022 Server base.
docker build -t mssqlserver:2022-ltsc2022 --build-arg SQL_VERSION=2022 --build-arg WIN_VERSION=ltsc2022 .
Build MSSQL Server 2019 with Windows 2019 Server base.
docker build -t mssqlserver:2019-ltsc2019 --build-arg SQL_VERSION=2019 --build-arg WIN_VERSION=ltsc2019 .
Build MSSQL Server 2019 with Windows 2022 Server base.
docker build -t mssqlserver:2019-ltsc2022 --build-arg SQL_VERSION=2019 --build-arg WIN_VERSION=ltsc2022 .
Build an MSSQL Server in Windows Container image using GitHub action
Fork the squirrelistic-blog repo.
Go to forked repository settings => secrets => actions and create 3 secrets, pointing to your private Docker repository.
- REGISTRY_LOGIN_SERVER
- REGISTRY_PASSWORD
- REGISTRY_USERNAME
Go to actions, select 'Build and Push MSSQL Server Windows Docker Image' and click 'Run Workflow'.
Select the desired version of MSSQL and Windows Server base, and it should be cooked and ready to eat in about 20 minutes.
How do I run this thing?
Run the MSSQL Server with a specific password (the default password is PleaseChangeMe!) and use your own custom MSSQL TCP port (8443).
Make sure the password adheres to the MSSQL Server password policy.
docker run --rm -it -p 8433:1433 -e sa_password=DictionaryNinja! mssqlserver:2022-ltsc2019
You can now log in to MSSQL using SQL Management Studio.
If you want to attach a database at the start of the container, you can use attach_dbs environment variable.
docker run --rm -it -p 8433:1433 -v c:/Temp/DataDirOnHost:c:/SqlData -e attach_dbs="[{'dbName':'Hello','dbFiles':['C:\\SqlData\\hello.mdf','C:\\SqlData\\hello_log.ldf']}]" mssqlserver:2022-ltsc2019