Create Docker Container for MSSQL on Mac


As the first step we need to install Docker Mac and make sure you have Rosetta get installed, in case of using Mac with M1 or M2. These Models come with Apple Silicon. Some of the older apps come with Intel chip support, Rosetta will help you the migrate to Apple Silicon.

Docker Compose

Using the Docker-Compose we can containerise MSSQL. Here is the working Docker-Compose file.

Create a Folder called MSSQL and add Docker-Compose.yml file with following contents

version: '3.9'

services:
  mssql-azur-edge:
    image: mcr.microsoft.com/azure-sql-edge 
    platform: linux/arm64
    ports:
      - 1433:1433
    volumes:
      - ~/apps/mssql/data:/var/lib/mssqlql/data
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=mssql1Ipw

Since I’am using the Mac Min M2, which is an arm device, should specify in the platform key. Port key expose the internal port to external. Volume indicate the storage location which is optional.

In the environment key we can pass the Password for SQL Server etc.

Now . Run the Docker-compose up -d. for running the Container. If there is any problem , run this command for the log docker-compose logs

You are ready to use the sever. You can also use the Mac IP and exposed port in SQL Management studio or in a connection string as (source)

 Mac IP, 1433

Docker-compose for projects

How use docker-compose to run MySQL database base for development


In the past docker posts we used docker run command to run containers using docker image and also the Dockerfile to create project image.

Sometimes we need to use/run a database in container during development, in such cases docker-compose came into light.

The Docker-Compose file

Docker-compose is a file at the root of your project, which has no extension and carry some special script. In our example we are configuring MySQL database.

version: '3.1'
services:
  db: 
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment: 
      MYSQL_ROOT_PASSWORD: example
      MYSQL_USER: development
      MYSQL_PASSWORD: development
      MYSQL_DATABASE: blog
    ports: 
      - 127.0.0.1:3306:3306

Up and Down

To start the docker-compose use the up command from the project root

docker-compose up -d

The down command used to remove and stop for stopping the container .

Checking the docker-compose containers

For listing the containers we can use

docker-compose ps

Accessing MySQL CLI

Login into the database and create some database ( as root)

docker exec -i some-id sh -c "exec -uroot -p password"

You can also use -u username for login as normal user and can execute basic sql operations such create database, tables and CURD operations on objects etc. Note that the database uses the default port of MySQL

We can stop running a container at any time using docker stop <id/name> and resume with docker start <id/name>

Following Docker Post may deserve a good read