Script Bytes

Tutorials and Tips for Angular, .Net, and More

Run SQL Server on M1 or M2 Macbook

Jeff F
Jeff F
Cover Image for Run SQL Server on M1 or M2 Macbook

Did you know that it's possible to run SQL Server on your M1 or M2 Macbook? Well it is…sort of. Microsoft provides a docker image, which lets you run it on non-windows computers. Unfortunately, they don't provide an image using the ARM processor architecture, but they do provide a lighter version of SQL Server called SQL Edge, and it does support ARM. I'm going to show you how to run this version of SQL Server on your M1 or M2 ARM Mac.

This video also covers the same topic of running SQL Edge on an M1 or M2 Mac:

Run SQL Server on M1 or M2 Macbook

Pull SQL Edge Docker Image

The first thing you need to do is pull the docker image for SQL Edge. I'm assuming you already have Docker installed.

Here is a link to the Docker Hub page for SQL Edge for reference.

Pull the image by opening up a terminal and running the command docker pull mcr.microsoft.com/azure-sql-edge

Run SQL Edge Container

Once that is done downloading, you can run the container using this command:

docker run --cap-add SYS_PTRACE -e 'ACCEPT_EULA=1' -e 'MSSQL_SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 --name azuresqledge -d mcr.microsoft.com/azure-sql-edge

Make sure you change the password to the password you would like to use.

If you aren't familiar with Docker Run commands, here is a breakdown of all the pieces of the above line:

  • --cap-add SYS_PTRACE: Microsoft's note on this line: The --cap-add SYS_PTRACE flag is required for non-root SQL Server containers to generate dumps for troubleshooting purposes.
  • -e 'ACCEPT_EULA=1': An environment variable, which is saying that we will accept the End User License Agreement.
  • -e 'MSSQL_SA_PASSWORD=yourStrong(!)Password': This is setting the password for the sa (the system administrator) user. This is how you will log into the database. By default the username is 'sa'.
  • -p 1433:1433: Mapping ports. The left side of the colon is the port on the host (your mac). The right side is the port used inside the container.
  • --name azuresqledge: Just giving the container a more user-friendly name after it starts running.
  • -d: Detached mode. This means that the terminal won't attach to the process, which means it'll start the container up and then give you terminal control back, so you can run other commands.
  • mcr.microsoft.com/azure-sql-edge: The name of the image. This is the same name used in the docker pull command.

That's all there is to it. You should now be able to log into your database using the username=sa and password=YourPassword.

Persisting Data

One downside to the way the container was run above is that the data is not being persisted. That means that if you remove that container in docker, all of the data inside of it will be deleted. You can stop the container and restart it and be ok, but if you delete it, your data is deleted too.

That obviously isn't ideal in most cases, but there is an easy solution: Volumes.

Docker gives you a way to map a folder inside of the container to a volume on the host. This way you can use that same volume every time you run it and the data will persist.

Here is a link to Microsofts documentation on Persisting data for reference.

The above link shows a couple different ways, but the easiest way is to use a "data volume container", or a named container. To do this we need to add one more option into our docker run command -v sqlvolume:/var/opt/mssql

-v means we are passing a volume. The format is similar to how we define ports: Host:Container. On the left, the name 'sqlvolume' tells docker to use, or create, a volume with that name on your computer, the host. Then on the right, it's saying to map it to /var/opt/mssql. This is the folder in the container where SQL stores all of its data files.

This is basically telling docker "Everything that you write to /var/opt/mssql is going to be written to a volume named sqlvolume, and that is stored on my host machine."

As long as you run the container with this same name, your data will still be there.

The name of the volume can be any name you choose, it doesn't have to be sqlvolume. Just be sure to use the same name every time you run the container.

Summary

I hope you found this post helpful. I wanted to also mention that SQL Edge does have some limitations compared to the full version of SQL Server, but more than likely it will be acceptable for running the database locally for development and testing.

Here is a comparison of the features.

Thanks for reading!