Here’s how to do it from the comfort of your operating system.
The purpose of this article is to present the options for setting up a local development environment for ASP.NET Core and SQL Server on MacOS, without having to spin–off a Windows virtual machine.
Luckily, multiple options are now available for running and developing with .NET on MacOS, either directly by installing .NET Core on MacOS or by running one of the containers exposed by Microsoft here: https://hub.docker.com/_/microsoft-dotnet-core/
The focus will be on SQL Server, which (starting with version SQL Server 2017) can be run in a Docker container, meaning it’s no longer necessary to run a full virtualization software (e.g., Parallels, VMWare).
Docker is a platform that enables software to run in its own isolated environment. It’s a pre-req for being able to run SQL Server 2017 or newer on a Mac.
If you haven’t done this already, go to https://hub.docker.com/editions/community/docker-ce-desktop-mac?tab=description to grab the free Docker community edition for MacOS. You have the option to grab the Stable or the Edge version.
Whichever you decide to get, after downloading, install the .dmg file and then drag the app to your local Applications folder. After that, it can be launched like any other MacOS application (from Launchpad, Applications folder or Spotlight Search). Once opened, you should first configure the resources—especially the RAM and number of CPUs. By default, Docker will allocate 2GB of RAM, but you can change this based on your physical machine configuration and the rest of the utilization. However, if you can, you should increase the SQL Server RAM to 4GB to improve performance. This is done by accessing the Preferences option from the drop-down menu.
You can also play with various configurations—but any change in config warrants a restart of Docker—by clicking “Apply & Restart.”
a. Installing .NET
One option is to install .NET Core runtime and SDK directly to the MacOS. The SDK can be downloaded from here: https://dotnet.microsoft.com/download/dotnet-core. At the time this article was written, the latest version is 3.1, downloadable from here: https://dotnet.microsoft.com/download/dotnet-core/3.1. You have the option of using an installer or you can download and manually install, or you can install with Visual Studio for Mac (https://docs.microsoft.com/en-us/visualstudio/mac/installation?view=vsmac-2019)<
or alongside of Visual Studio Code for Mac. That is, after installing Visual Studio Code, you have to add .NET Core from the marketplace: https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp
b. Running a Docker .NET Core container
If you don’t want to add .NET Core to your MacOS, you still have options, since .NET Core can also run in a Docker container. Official .NET Core Docker images are published to the Microsoft Container Registry (MCR) and are discoverable here: https://hub.docker.com/_/microsoft-dotnet-core/
In order to run a pre-build container with .NET Core, execute the following:
docker run -it –rm -p 8000:80 –name aspnetcore_sample
You can now navigate in your browser here: http://localhost:8000/
Pretty sweet, right? A working .Net Core with a minimum of effort. If you check the Docker dashboard, you’ll see the asp.net samples container added to the list of containers:
c. Install SQL Server
In a similar manner, you can pull a SQL Server container by running:
docker pull microsoft/mssql-server-linux
This will pull the latest Microsoft containerized version of SQL Server for linux. At the time I wrote this, after running the command above, I got version:
Microsoft SQL Server 2017 (RTM-CU13) (KB4466404) - 14.0.3048.4 (X64) Nov 30 2018 12:57:58 Copyright (C) 2017 Microsoft Corporation Developer Edition (64-bit) on Linux (Ubuntu 16.04.5 LTS)
In order to run it, the command becomes:
docker run -d –name <your_container_name> -e ‘ACCEPT_EULA=Y’ -e ‘SA_PASSWORD=<your_password>’ -p 1433:1433 microsoft/mssql-server-linux
This optional parameter launches the Docker container in daemon mode. This means that it runs in the background and doesn’t need its own Terminal window open. You can omit this parameter to have the container run in its own Terminal window.
Another optional parameter. This parameter allows you to name the container. This can be handy when stopping and starting your container from the Terminal.
The Y shows that you agree with the EULA (End User License Agreement). This is required in order to have SQL Server for Linux run on your Mac.
Required parameter that sets the sa database password. It is advised to be a strong password
This maps the local port 1433 to port 1433 on the container. This is the default TCP port that SQL Server uses to listen for connections.
This tells Docker which image to use. mssql-server-linux is the image downloaded with the pull command above.
d. Run SQL Server in a container
Now the SQL Server is installed, let’s see how it behaves. First, let’s check its status. For that, type:
If it’s up and running, it will return something like this:
d9314e641671 microsoft/mssql-server-linux "/opt/mssql/bin/sqls…" 47 hours ago Up 47 hours 0.0.0.0:1433->1433/tcp sql_server_demo
where the first value represents the Docker container ID.
e. Interact with SQL Server container instance
One fast way to interact with the newly created SQL Server instance, is via the command line. For that, you first need to install sql-cli
sudo npm install -g sql-cli
Of course, this will only work if you already have NodeJs installed. If not, install it from here https://nodejs.org/en/, then run the command above (Node Package Manager (npm) will be automatically installed with NodeJs)
Now that the cli is installed, you can start querying the database. First, let’s connect. For that, type:
mssql -u sa -p <your_password>
You should see something like this:
Connecting to localhost...done sql-cli version 0.6.2 Enter ".help" for usage hints. mssql>
Now, let’s run a query, such as select @@SERVERNAME. This will produce the output:
mssql> select @@SERVERNAME ------------ d9314e641671 1 row(s) returned Executed in 1 ms mssql>
Notice that the SQL instance name coincides with the Docker container ID
2. Azure Data Studio
There are also other ways to communicate with the database – such as a GUI. The most common one is Azure Data Studio, downloadable from here: https://docs.microsoft.com/en-us/sql/azure-data-studio/download-azure-data-studio Of course, you have to grab the MacOS version (https://go.microsoft.com/fwlink/?linkid=2142136). After download, just expand the compressed file and then move the app to the Applications folder on your Mac. At this point, just open it like any other Mac application.
One of the first things to do is to connect to a database. Let’s connect to the database the we just created earlier. Go to “New connection” which will prompt:
Notice that for Server field I used “.” This ensures it will go against the local instance.
Once connected, you can add databases, work with existing ones and so on.
A very important operation is to restore an existing database. I’ll explain how it works for a containerized database.
3. Restore an existing database
Before being able to even invoke the Restore command, you need to enable preview features. To do that, you have to go to “Settings,” search for “preview,” scroll to the bottom and check “Enable unreleased preview features.” After this, you’ll be able to restore a database.
Now, before restoring, the problem is: how to bring the image file (typically a *.bak file) to the container?
First, you need to move the file from the host operating system file system to the container file system. Let’s grab the container id by running “docker ps” as explained earlier. Grab the ID and then run
tar -cv <your_image_file> | docker exec -i d9314e641671 tar x -C /var/backups
Here d9314e641671 is the container ID and <your_image_file> is the backup file you need to transfer from your local system to the container. This will copy the image file to the container file system in the /var/backups folder, so when executing the actual restore operation, you’ll have access to the file (as in the image below).
After restoring, the newly restored database should show up under the “Databases” folder.
Those are the most common ways to get set up on a Mac for .NET and SQL Server development – without spinning off a virtual machine. I hope you found them useful.
For further reads on what can else be achieved with Docker and Visual Studio, such as Container Tools, deploying containers, check out this Microsoft page. Lastly, if your organization has any additional questions about getting set up for .NET and SQL Server development, without spinning off a virtual machine, please don’t hesitate to reach out to us. We’d love to help you get started.