Hey folks, today I'm going to show you how you can run SQL Server on an M1 or M2 Mac. And you can do this by using Docker. In one of my previous videos, I showed how to do this, but in that video I was recording on a Windows machine. And after I recorded that video, I had a few comments saying that people are having issues doing the same thing on their Mac.
Well, not too long after I recorded that video, I actually switched my main development computer over to be a MacBook. So I'm going to use that MacBook to do this demo. So let's go ahead and get started. So if you've tried to run SQL Server and Docker on your MacBook, you may have noticed that it doesn't work and that's because SQL Server doesn't have a Docker image that uses the ARM processor. But there is an alternative from Microsoft.
It's a database called Azure SQL Edge. And using this database they do have an option for ARM processors. So Azure SQL Edge has most of the features that SQL Server does but not all of them.
The database is mostly intended for IoT devices. However for development purposes you'll probably be just fine using SQL Edge and won't have any issues. Microsoft does have a page that shows the features that are and are not supported in Azure SQL Edge, so I'll put a link to that down in the description below if you want to go check and make sure that you won't have any problems. So to get started, the first thing we need to do is pull down the image. So if you head over to the Azure SQL Edge Docker page, you can copy this command right here, open up a terminal, and run that.
So I already have this on my computer, so it was pretty much instant, but if you don't it'll take a few minutes for this to download. If you scroll down they have the docker run command you need to use to start this container. So we'll copy that paste that into your terminal. The one thing you do need to change is the password.
So put in the password you'd like to use. So by default I do believe this database will require strong passwords. So just make sure you have a capital letter, it's a long enough password, and that you have a number or a symbol in it.
So for my password I'll use like and subscribe. So I'll go over all the pieces in this docker run command. So we're using docker the command is run. And cap add is just a way of saying add Linux capabilities to this container.
And sysptrace, according to their documents, it says trace arbitrary processes using ptrace. I'm honestly not sure what that does. Next, dash e means an environment variable. And we're saying accept EULA equals one. So EULA is the end user license agreement.
So this is just passing in an argument saying, yes, I agree, since you can't click an actual button on it. Then we have another environment variable, which is the sysadmin password. Mine's like and subscribe. Dash P is port mapping. So we're going to map port 1443 on the host to port 1433 inside the container.
Lastly we're giving it a name called Azure SQL Edge. We're going to run it in detached mode. And detached mode just means that after we run this it's not going to continue to use the terminal.
It'll just say it's running and then we'll get the terminal back so we can continue using it if we need to. And lastly is the name of the image. which is the same name of the image that we pulled right up here.
So you will go ahead and run this and you can see it printed out the container id which that just means that it's running. And so I'll switch over to docker desktop just to show that that container is running. So under containers here is that container azure sql edge that is running. It's been running for 52 seconds and now this database is up and running so we can go ahead and connect to it and use it just like any other database.
And so really quick I'll just open up azure data studio just to make sure that we can connect to it. So I'll say new connection. It is Microsoft SQL Server.
The server name is localhost. By default the username is going to be sa unless you specify it differently, which we didn't. The password is the password that you used. Then go ahead and connect and it connected. So you can see here I can expand databases.
It's got all the default SQL databases inside of it. And so now this database is ready to go. You can start using it for development purposes.
So the one downside of this container with how we ran it in this example is that if you turn it off and delete the container you will lose all of the data that's inside of that. So all of your tables, all of your data will all be gone. So if you want to make sure that you are persisting your data between spinning up and spinning down this container, you need to pass in a volume.
And this is really easy to do. Microsoft has great documentation on this, so I'll put a link to this article down in the description. So if you go to the configuration page for Azure SQL Edge, they have a section which is just for persisting data. And in my last video, I showed how to do this on the Windows machine, and I mounted those volumes as host directories. And so what that means is that we told it to map these different folders in the container into a folder or into a specific folder on our host.
And unfortunately you can't do that with the Mac version. So you can see here they have a little important message saying host volume mapping for Docker on Mac with Azure SQL Edge is not supported at this time. But what it does say is use data volume containers instead.
And right below that they show how to do this. And this is actually a little bit of an easier way to do it honestly. And the way that works is instead of telling it which folder to use on your host, you just tell Docker, hey, create a volume with a specific name and use that volume for all of my data.
So in this example here, you can see they're just given a name. The name is SQL volume, and they're saying map that to this folder inside of the container. So as long as you use this SQL volume name every time you start the container, your data is going to be there.
So this is what we're going to do. Let's just copy this little piece right here. I'm going to go into Docker Desktop. I'm going to stop and delete my container.
And we're going to run the same command that we did earlier, and we're just going to add that one little piece. So now I have... a volume called SQL volume and I'm mapping it to var opt ms sql inside the container. So we can go ahead and run that it starts back up and now you can persist your data even if you shut down and delete your container. And there's a couple different ways that you can check the volumes that are currently being used or currently exist inside of your your docker environment.
So one way is using docker desktop. So on the left here if you go to volumes it'll list all of the volumes that are currently in use. And you can see here SQL volume is the one that we just created.
It's in use. It was just created a minute ago and it's about 70 megabytes in size. And another way you can do this is using the command line. So if you type docker volume list and it'll list out all of the volumes and their names. And so now you're all set for running Azure SQL Edge on your M1 or M2 Mac.
I do want to show one more thing to maybe help out people who are having issues when they're starting their Docker containers. And that's how to look at the logs inside the container and hopefully get some information about what's going on and why it's not starting up for them. There's a couple ways that you can do this.
And so the first way is again by using Docker Desktop. So if you switch over to Docker Desktop and you go up here to containers, if you click on the container it'll open up the logs for that container. And so if you're having issues logging in or starting your container, this will probably give you a little bit of information about what's going on. And so I'm going to give just a couple examples of what kind of information you might be able to see in here.
So I'm going to go back to my container and I'm going to stop it and I'm also going to delete it. Now I'm going to go back to my terminal. I'm going to go back to the docker run command.
I mentioned earlier you have to have a strong password. So I'm going to give it a weak password and just see what it says. So you can see it started up or it looks like it started up.
Oh interesting it actually is running. Okay there we go. That's why it's not working. So as I was recording this, I actually found out kind of an interesting thing about how these volumes work. So I was going to show you how you can rerun this docker run command and give it a different password to try to, you know, make it fail.
And it wasn't failing, it just was working with the old password. And I realized just because the volume is still there and persisting data. So I'm telling it to run and I'm telling it to use that volume and that volume already knows what the password is because all the data is already in there.
So if you need to make configuration changes like this, you need to actually delete that volume and then recreate everything. And so one way you can do that is you can do it through Docker desktop by going to volumes and deleting it, or you can also do it through the command line. So docker volume and then if you say rm for remove and then you give it the name, oh it's in use. So say docker container rm and then 6 6 fa.
So that removes the docker container. So now I can rerun the remove command for the volume and that removes it. You can confirm that if you want by listing the volumes.
So now what I want to do is rerun the docker run command with a weak password. So you can see the password now is just like if I run this. It looks the same. It spit out the id of the container and it looks like it's running. However, if you go and you watch docker desktop you can see that it exited with a code of one, which means it's exited so it's not running anymore.
So if we go ahead and click on it, you can see it says disconnected from SQL Server. Enable the sysadmin password. Password validation failed.
The password does not meet the SQL Server policy requirements because it's too short. So this is one example of what type of error might show up and how you can figure it out. So now we know we need to give it a better password. So I want to show one other way that you can view the logs without using Docker Desktop.
And this is great if you are running this on a server and don't have access to the GUI and you're doing everything through the command line. So if you run the docker container and you take off the"-d", for detached mode, this will stay attached and it will actually print out all the logs right here in this terminal for you. So if we run that, these are the same logs that we saw inside of docker desktop, but now it's just showing all of them in the terminal instead. So now I'm going to show you one other way that you can view the logs using the command line.
which is using the docker logs command. So if you say docker logs and then you give it the id of the container, which right now our container running starts with b3e. So if I say docker logs b3e, this will print out all the logs that are currently available.
So this is great just to get a quick snapshot of what's been logged. Now there's one other way that you can do this which is docker logs and then you pass in dash f which stands for follow. And what this does is this will actually continue to print out new logs as they come in.
So if you're trying to debug something, you can turn this on, you can go use the database, you can see what's happening, and you might be able to get a better idea of what's going on. So I hope you guys found all this information useful. If you did, please consider liking and subscribing.
If you guys notice any issues or have any questions or if I did something incorrectly, please let me know in the comments down below. And thanks for watching and I'll catch you later. Also I'm sorry if I'm kind of nasally. I've got a little bit of a cold so I don't sound that great. I mean I don't sound that great anyways but what can you do?