Using SQL Server on M1 based Mac with Docker

In this blog, we will learn about:

  1. Installing Docker on M1 based Mac
  2. Installing alternative of SQL Server management studio i.e. Azure Data Studio
  3. Installing SQL Server using Docker image
  4. Using SQL Server in Azure Data Studio

Step 1: Download Docker for Mac M1.

You must be aware that Docker is not natively supported by M1 based Macs. Docker development team is still working on the stable version, but they had released a Tech Preview for the Developers around the globe to help them test their development quickly and they can address as many bugs that they can. 

For the purpose of this tutorial, I am also going to download the same Tech Preview on my M1 based Mac and will try to run SQL Server images on this.

Go to https://docs.docker.com/docker-for-mac/apple-m1/ and download the latest preview build available on the website. 

Once you download it, install the docker preview in your machine. You should be able to view the docker desktop.

Step 2: Download an IDE to run the SQL commands and perform other DB operations. 

The best tool for SQL Server is SQL Server Management Studio (SSMS), but bad luck is that it is only supported on Windows OS. 

Thanks to the Microsoft team, they provided an alternative to SSMS. The tool name is Azure Data Studio. We will use this tool in our M1 based Mac.

Go to https://docs.microsoft.com/en-us/sql/azure-data-studio/download-azure-data-studio?view=sql-server-ver15#get-azure-data-studio-for-macos and download the installer.

Step 3: Create a new ID on https://hub.docker.com or login using your existing ID.

Once you are logged in, just hit https://hub.docker.com/_/microsoft-azure-sql-edge and you will a docker image hosted by Microsoft with name Azure SQL Edge.

We will download this image on our local system using docker commands and then run the image in the docker container on localhost port. Once we run the image in the container, we will be able to use SQL server on our M1 based Mac.

Step 4: Download the docker image using MacOS terminal. 

Open the terminal in your system and hit command: docker pull mcr.microsoft.com/azure-sql-edge

Once you hit the above command, docker will start pulling the image from online repository and download this on your local system. 

You can also see the image in the Docker Desktop window as shown below.

Step 5: Once you download the image in the docker, you need to run this image within a Docker container on some active localhost port.

Use the same terminal window and hit the following command.

docker run –cap-add SYS_PTRACE -e ‘ACCEPT_EULA=1’ -e ‘MSSQL_SA_PASSWORD=Password.1’ -p 1433:1433 –name azuresqledge -d mcr.microsoft.com/azure-sql-edge

  • -e belongs to environment variable.
  • ACCEPT_EULA=1 is the acceptance of license agreement.
  • MSSQL_SA_PASSWORD=Password.1 is the password of the SQL server user with username ‘sa’.
  • -p belongs to port number which we have set to 1433.
  • –name belongs to the name of the docker container that we want to create in Docker.

Once the command is successful, you can go to the container option in the docker desktop window and you will find a container running the with same name that we passed in the command.

This makes sure that SQL server is now running on our system.

Step 6: Now the next step is to use the SQL server using Azure Data Studio.

Open Azure data studio and click on “Create a Connection” option. Enter the details as shown in the screenshot below.

Fill the details as shown below.

Once you click on Connect, you will be able to see that we are able to connect to local Sql server instance. 

Now, you can use new query option to write queries to generate tables, stored procedures, etc.

30 thoughts on “Using SQL Server on M1 based Mac with Docker

  1. Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 0 – 37379319)
    —> System.ComponentModel.Win32Exception (373793196): Unknown error: 373793196
    at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
    at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
    at Microsoft.Data.SqlClient.TdsParserStateObject.ThrowExceptionAndWarning(Boolean callerHasConnectionLock, Boolean asyncClose)
    at Microsoft.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
    at Microsoft.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync()
    at Microsoft.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket()
    at Microsoft.Data.SqlClient.TdsParser.ConsumePreLoginHandshake(Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean& marsCapable, Boolean& fedAuthRequired)
    at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType)
    at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
    at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
    at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
    at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool)
    at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
    at Microsoft.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
    at Microsoft.Data.ProviderBase.DbConnectionFactory.<>c__DisplayClass48_0.<CreateReplaceConnectionContinuation>b__0(Task
    1 _)
    at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    — End of stack trace from previous location —
    at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
    — End of stack trace from previous location —
    at Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection.ReliableSqlConnection.<>c__DisplayClass30_0.<b__0>d.MoveNext() in D:\a\1\s\src\Microsoft.SqlTools.ManagedBatchParser\ReliableConnection\ReliableSqlConnection.cs:line 316
    — End of stack trace from previous location —
    at Microsoft.SqlTools.ServiceLayer.Connection.ConnectionService.TryOpenConnection(ConnectionInfo connectionInfo, ConnectParams connectionParams) in D:\a\1\s\src\Microsoft.SqlTools.ServiceLayer\Connection\ConnectionService.cs:line 602
    ClientConnectionId:402154bd-54c3-4953-bc0d-1f579964eb37
    Error Number:373793196,State:0,Class:20

    Like

  2. bro, you forgot one minus in front of “cap-add” in the terminal command, when you tried to run image. Thats why I had problems. So the correct way would be:

    sudo docker run –cap-add SYS_PTRACE -e ‘ACCEPT_EULA=1’ -e ‘MSSQL_SA_PASSWORD=bigStrongPwd’ -p 1433:1433 –name sqledge -d mcr.microsoft.com/azure-sql-edge

    Like

  3. Hello. All steps went well except the last one. When I write the localhost, sa and the password, it doesn’t connect.

    And I see this error: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 – Could not open a connection to SQL Server: Could not open a connection to SQL Server).

    Do you have any idea how to fix this?

    Like

    1. besides in docker I see this: WARNING: Failed to connect to SQL because: dial tcp 127.0.0.1:1431: connect: connection refused, will reattempt connection.

      Like

  4. Hi, could you please upload a tutorial for the adventure works?
    I’m having trouble with the following part:
    In your YouTube for apple with intel , the code was sudo docker exec -it ms-sql-server mkdir var/opt/mssql.backup
    but my terminal showed Error: No such container: ms-sql-server
    Then, I tried sudo exec -it azuresqledge mkdir var/opt/mssql/backup
    and my terminal showed sudo: exec: command not found

    Like

  5. hi, When I tried to create a new connection with the given credentials I received this error:
    — Login failed for user ‘sa’
    I never changed username or password.. I tried different names and password but it is not still working.. can anyone help me please?

    Like

  6. Hi, received this error
    [launchpadd] WARNING: Failed to connect to SQL because: dial tcp 127.0.0.1:1431: connect: connection refused, will reattempt connection.

    Container is not starting. Did anyone face this issue?

    Liked by 1 person

    1. same on me, have you fixed this issue?
      Failed to connect to SQL because: dial tcp 127.0.0.1:1431: connect: connection refused, will reattempt connection.

      Like

      1. delete the container and write this in the terminal instead : (there is sth wrong in the ‘ )
        docker run –cap-add SYS_PTRACE -e ‘ACCEPT_EULA=1’ -e ‘MSSQL_SA_PASSWORD=Password.1’ -p 1433:1433 –name azuresqledge -d mcr.microsoft.com/azure-sql-edge

        Like

  7. Hi there,
    When I tried to create a new connection with the given credentials, it showing error.
    I haven’t used password anywhere to create.
    can someone please help me to resolve this issue.
    Thanks

    Like

    1. Hi Sahith,

      Maybe you used different credentials while running the container. To check this, just go to your docker container instance and click on Inspect tab. The tab will display the credentials that needs to be used for connecting to SQL server instance.

      Like

  8. For anyone else getting the “invalid reference format” error when pasting in the command from this article, it’s because the double-hyphens (–) are being replaced with a single dash on the ‘cap-add’ and ‘name’ arguments. Just swap them back to double hyphen and it works fine @Will Appleby

    this worked for me you have to paste the command in terminal and then go back and remove -cap-add to –capadd, you will notice when you do that it will do two of them .l you have to for both of these

    Like

  9. Hi everyone, I have tried all the commands above. Each time, I quit terminal and re-try with a fresh command, but it is still not working.

    My errors:

    See ‘docker run –help’.
    zsh: event not found: )Password’

    Like

  10. I have tried with all the examples above, and I still receiving docker: invalid reference format.
    See ‘docker run –help’.

    Like

  11. Guys use this command it will definitely gonna work for you
    docker run -e “ACCEPT_EULA=1” -e “MSSQL_SA_PASSWORD=MyPass@word” -e “MSSQL_PID=Developer” -e “MSSQL_USER=SA” -p 1433:1433 -d –name=sql mcr.microsoft.com/azure-sql-edge

    Liked by 1 person

  12. For anyone else getting the “invalid reference format” error when pasting in the command from this article, it’s because the double-hyphens (–) are being replaced with a single dash on the ‘cap-add’ and ‘name’ arguments. Just swap them back to double hyphen and it works fine

    Liked by 1 person

  13. The correct command to get around the stupid ‘invalid reference format’ is this:

    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

    Note — before cao-add and name? apparently that is important! This is why code will be delegated to a handful of people and no-code will be mainstream soon! 😀

    Liked by 1 person

      1. When I adjust the command to include the — in front of cap-add and name, I just get this error:

        unknown shorthand flag: ‘â’ in -–cap-add

        Like

  14. When I try to run I get “docker: invalid reference format”. I’m using Docker Desktop 3.3.1, which is currently the latest for Apple silicon. I’ve made sure that all special characters (dash, quote, etc) are correct, but still I cannot get Docker to accept my command. Is there any difference between 3.3.1 and 3.1.0 that could explain this? Or is there in other known problem?

    Like

  15. For anyone not able to run the docker run command. In the documentation at microsoft documentation it says the terminal command should be ran with super user. It worked then!

    sudo 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

    Liked by 1 person

      1. I got a kernel panic, and by accident factory reset docker (bad luck click of box which showed up at worst timing possible).

        Now I’m getting this error: docker: invalid reference format.

        Any ideas? I’ve tried setting it all up from scratch, but that specific run command does not work.

        Like

  16. My previous comment can’t be deleted by me so you can delete both if you’d like 🙂

    Solved: Docker wasn’t starting. Had to click the bug icon top right, purge and factory reset. Restarted the app and then it finally booted fully. I was trying to do the docker pull from OS terminal with docker installed via brew. It did NOT work. Entered the command inside of Docker and it ran just fine.

    Like

  17. memiru@MemiruMBP ~ % docker pull mcr.microsoft.com/azure-sql-edge
    Using default tag: latest
    Error response from daemon: dial unix docker.raw.sock: connect: no such file or directory

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.