Run MSSQL 2017 server in docker for windows

MSSQL 2019 not available yet in official images.

Docker compose

First check attach_dbs argument. The first time running it, remove it. Afterwards, if starting with an existing db, use it to attach existing mdf/ldf files. They can also be attached from T-Sql after starting the server.

Note: Mapping localhost folder to default C:/Program Files… fails, probably for permission reasons (not clear from the message, as usual).

// docker-compose.yaml
version: '3.8'
services:
  SQLServer:
   image: microsoft/mssql-server-windows-developer:2017
   environment:
     - ACCEPT_EULA=Y
     - SA_PASSWORD=Password_01
     # - attach_dbs=[{'dbName':'Joker','dbFiles':['C:\\SQLData\\Joker1.mdf','C:\\SQLData\\Joker1.ldf']},{'dbName':'Joker2','dbFiles':['C:\\SQLData\\Joker2.mdf','C:\\SQLData\\Joker2.ldf']}]
   ports:
     - '1433:1433'
   volumes:
     - ./data:c:\SqlData

Create database specifying the physical location of files that is already mapped to host’s.

CREATE DATABASE Joker
ON
( NAME = Joker,
    FILENAME = 'C:\SqlData\joker.mdf',
    SIZE = 10,
    MAXSIZE = 50,
    FILEGROWTH = 5 )
LOG ON
( NAME = Sales_log,
    FILENAME = 'C:\SqlData\joker.ldf',
    SIZE = 5MB,
    MAXSIZE = 25MB,
    FILEGROWTH = 5MB )

Test it.

USE Joker 

CREATE TABLE FunnyJokes (
  Id INT   NOT NULL    IDENTITY    PRIMARY KEY,
  Title   VARCHAR(100)  NOT NULL,

);

INSERT INTO FunnyJokes (Title) 
VALUES ('The one with the tiger');