Setup Local SQL Server Server on Docker - gecko-8/devwiki GitHub Wiki

Up

Setup

  1. Download and install Docker for Mac (https://www.docker.com/)
  2. Pull down the latest image for SQL Server (optional, will be pulled by next command if you don’t have it)
    sudo docker pull mcr.microsoft.com/mssql/server:latest
    
  3. Create and run the container
    sudo docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=<password>' -p 1433:1433 -v mssql:/var/opt/mssql -v <local db directory>:/var/opt/mssql/backups --name mssql-server -h sql1 -d microsoft/mssql-server-linux:latest
    
    Note: is a directory where you’ll be able to drop .bak files, etc. for easy access within the container. Example: /Users/MyData/Databases Note: This creates a Docker Container Volume with the name “mssql”.

Connecting

  1. To connect an external program, just use localhost, port of 1433, user of sa, and password of .
  2. To connect a .NET app, use the following connection string format
    Data Source=localhost;Initial Catalog=<database name>;User id=sa;Password=<password>;MultipleActiveResultSets=True
    

Deleting

  1. Stop the container
    docker stop mssql-server
    
  2. Delete the container
    docker rm mssql-server
    
  3. Delete the volume
    docker volume rm mssql
    
  4. Delete unused images (optional)
    docker image prune -a -f
    

Execute Script from

  1. Connect to the container with the command:
    docker exec -it mssql-server bash
    
  2. Navigate to the backups folder:
    cd /var/opt/mssql/backups
    
  3. Execute the script with the command:
    /opt/mssql-tools/bin/sqlcmd -U sa -P <password> -i <script file>
    
⚠️ **GitHub.com Fallback** ⚠️