run sql server through docker in Windows

description: run sql server through docker

start

  • open and start docker service
  • open Powershell

download sql server image

put following commend in powerhell

1
docker pull microsoft/mssql-server-linux:2017-latest

start sql server

Asdfgh123 is default password

1
2
3
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Asdfgh123" `
-p 1433:1433 --name sql1 `
-d microsoft/mssql-server-linux:2017-latest

if get following error:

1
2
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/sql1" is already in use by container "1da5029c71ab937a25acdb4fadf44f4065c2fea4f6a4f157c1a07516558757bb". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.

restart the server

1
docker start sql1

connect to server

1
docker exec -it sql1 "bash"

start sqlcmd tool

1
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Asdfgh123'

test create database

1
2
3
CREATE DATABASE TestDB
SELECT Name from sys.Databases
GO

test acquire database

1
2
3
4
5
6
USE TestDB
Create new table named Inventory:
CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
SELECT * FROM Inventory WHERE quantity > 152;
GO

connect from external tools

outside the container

1
sqlcmd -S 10.3.2.4,1433 -U SA -P "Asdfgh123"

Stop and remove container

1
2
docker stop sql1
docker rm sql1

run sql server with external folder

1
2
3
4
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Asdfgh123" `
-v $PWD/:/data `
-p 1433:1433 --name sql2 `
-d microsoft/mssql-server-linux:2017-latest

more on search4fan.github.io