Configure SQL Server in docker for Mac

I work predominantly in C# and ASP.Net and I am more comfortable working with Microsoft SQL server in all projects. My personal laptop being a Mac its always a struggle to get it working especially with M1 and ARM. Docker always comes to the rescue in these situations and I am just listing down the steps here, so that I can come back to this blog later.

Have docker installed in your system if you don’t already have it. Once you have docker, type in the following command to get the latest sql server. At the time of this post, its 2022 for me.

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

Next run the following command, which actually installs the image with a default user SA and password configured. Make sure to use the password of your choice, but it needs to be a strong password which fulfills the sql requirements.

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=reallyStrongPwd123" -p 1433:1433 --name sql1 --hostname sql1 -d mcr.microsoft.com/mssql/server:2022-latest

This should list your containers and take a note of the container id

docker ps -a

You can also find the same in the GUI. Take note of the container id since we will be using it in next command. I always like to have the AdventureWorks sample database from Micosoft to be in the db. Allows me to quickly check some sql queries. For that, first download the Adventure works from the link below.

https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver16&tabs=ssms

Assuming its in the Downloads folder by default, use the below command to copy this into the folder in the container where sql server is installed. Change the folder path and container id to suit your needs.

docker cp ~/Downloads/AdventureWorks2022.bak {containerID}:/var/opt/mssql/data/

Now we need a client to connect to the server. I am using Azure Data Studio these days. Download and install Azure Data Studio and then click on New Connection.

With the above commands we have a default user SA. So enter Server as ‘localhost’, SQL Login with SA and password should connect to the server in docker container.

Remember we copied the AdventureWork2022 into the container folder. Next we need to restore database from that back up file. On bottom left of Data studio, we have settings, and then Command Palette. Type Restore in there and you should get the UI for selecting the type of restoration we need. A sample reference is given below:

And thats it. We have a sql server running in docker which has AdventureWorks2022 restored. Enjoy querying !

Leave a comment

Your email address will not be published. Required fields are marked *