From 2188003b5f25c0b4b19278497830fe62652a1355 Mon Sep 17 00:00:00 2001 From: Max Knor Date: Tue, 21 Jun 2016 15:12:52 +0200 Subject: [PATCH 1/4] Added custom DB ATTACH support to the start.ps1 as well as dockerfile. Changes to be committed: modified: dockerfile modified: start.ps1 --- .../dockerfile | 5 +-- .../start.ps1 | 32 +++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile b/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile index 8bd3e52b..e76236a8 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile @@ -12,6 +12,7 @@ MAINTAINER Perry Skountrianos # set environment variables ENV sql_express_download_url "https://download.microsoft.com/download/1/5/6/156992E6-F7C7-4E55-833D-249BD2348138/ENU/x64/SQLEXPR_x64_ENU.exe" ENV sa_password _ +ENV attach_dbs "[]" # make install files accessible COPY . / @@ -22,9 +23,9 @@ RUN powershell -Command (New-Object System.Net.WebClient).DownloadFile('%sql_exp RUN powershell -Command \ set-strictmode -version latest ; \ - stop-service MSSQL`$%sqlinstance% ; \ + stop-service MSSQL`$SQLEXPRESS ; \ set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpdynamicports -value '' ; \ set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433 ; \ set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ; -CMD powershell ./start %sa_password% \ No newline at end of file +CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\" \ No newline at end of file diff --git a/samples/manage/windows-containers/mssql-server-2014-express-windows/start.ps1 b/samples/manage/windows-containers/mssql-server-2014-express-windows/start.ps1 index 1fa234ab..f28672f1 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/start.ps1 +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/start.ps1 @@ -1,17 +1,43 @@ -# The script sets the sa password and start the SQl Service +# The script sets the sa password and start the SQL Service +# Also it attaches additional database from the disk +# The format for attach_dbs param( [Parameter(Mandatory=$false)] -[string]$sa_password +[string]$sa_password, + +[Parameter(Mandatory=$false)] +[string]$attach_dbs ) # start the service +Write-Verbose "Starting SQL Server" start-service MSSQL`$SQLEXPRESS - if($sa_password -ne "_"){ + Write-Verbose "Changing SA login credentials" $sqlcmd = "ALTER LOGIN sa with password=" +"'" + $sa_password + "'" + ";ALTER LOGIN sa ENABLE;" Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLEXPRESS" } +$attach_dbs = $attach_dbs | ConvertFrom-Json + +if ($null -ne $attach_dbs){ + Write-Verbose "Attaching database(s)" + Foreach($db in $attach_dbs) + { + $files = @(); + Foreach($file in $db.dbFiles) + { + $files += "(FILENAME = 'N$($file)')"; + } + + $files = $files -join "," + $sqlcmd = "sp_detach_db $($db.dbName);GO;CREATE DATABASE $($db.dbName) ON $($files) FOR ATTACH ;GO;" + + Write-Host Invoke-Sqlcmd -Query $($sqlcmd) -ServerInstance ".\SQLEXPRESS" + Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLEXPRESS" + } +} + while ($true) { Start-Sleep -Seconds 3600 } \ No newline at end of file From 919362cf60235cb43a1be6a1b767437933a155af Mon Sep 17 00:00:00 2001 From: Max Knor Date: Tue, 21 Jun 2016 15:30:12 +0200 Subject: [PATCH 2/4] Added additional comments to readme.md --- .../readme.md | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md b/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md index 24c76d71..16478eac 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md @@ -17,7 +17,7 @@ Note: This dockerfile is based on Buc Rogers' work that can be found [here] (htt ## About this sample 1. **Applies to:** SQL Server 2014 Express, Windows Server Technical Preview 4 or later -5. **Authors:** Perry Skountrianos [perrysk-msft] +5. **Authors:** Perry Skountrianos [perrysk-msft], Max Knor [knom] @@ -27,16 +27,44 @@ To run this sample, you need the following prerequisites. **Software prerequisites:** -You can run the container with the following command. Note the you'll need Windows Server Core TP5 v10.0.14300.1000. -docker run -p 1433:1433 --env sa_password= microsoft/mssql-server-2014-express-windows +You can run the container with the following command. +(Note the you'll need Windows Server Core TP5 v10.0.14300.1000.) + +```` +docker run -p 1433:1433 -v C:/temp/:C:/temp/ --env sa_password= --env attach_dbs="" -v microsoft/mssql-server-2014-express-windows +```` + +- **-p HostPort:containerPort** is for port-mapping a container network port to a host port. +- **-v HostPath:containerPath** is for mounting a folder from the host inside the container. + + This can be used for saving database outside of the container. ## Run this sample -The image provides one environment variable to set the sa password:
+The image provides two environment variables to optionally set:
- sa_password: Sets the sa password and enables the sa login +- attach_dbs: The configuration for attaching custom DBs (.mdf, .ldf files). + This should be a JSON string, formed like the following (note the SINGLE quotes and everything in one line): + ``` +[{'dbName':'MaxDb','dbFiles':['C:\\temp\\maxtest.mdf','C:\\temp\\maxtest_log. +ldf']},{'dbName':'PerryDb','dbFiles':['C:\\temp\\perrytest.mdf','C:\\temp\\perrytest_log. +ldf']}] + ``` + There can be zero to many databases in the array. + - dbName: The name of the database + - dbFiles: An array of absolute paths to the .MDF and .LDF files. + + Can be one or many, note that the path has double backslashes for escaping! + +This example shows all parameters in action: +``` +docker run -p 1433:1433 -v C:/temp/:C:/temp/ --env attach_dbs="[{'dbName':'MaxTest','dbFiles':['C:\\temp\\maxtest.mdf','C:\\temp\\maxtest_log. +ldf']}]" sqlexpr +``` + ## Sample details From 7acd16b67cbf21825e999507bb2d8e43498f9526 Mon Sep 17 00:00:00 2001 From: Max K Date: Tue, 21 Jun 2016 15:32:17 +0200 Subject: [PATCH 3/4] Updated readme.md --- .../mssql-server-2014-express-windows/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md b/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md index 16478eac..6b194926 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md @@ -62,7 +62,7 @@ ldf']}] This example shows all parameters in action: ``` docker run -p 1433:1433 -v C:/temp/:C:/temp/ --env attach_dbs="[{'dbName':'MaxTest','dbFiles':['C:\\temp\\maxtest.mdf','C:\\temp\\maxtest_log. -ldf']}]" sqlexpr +ldf']}]" microsoft/mssql-server-2014-express-windows ``` From 390b476f40718455d85d73ad74013c0025aaaa7d Mon Sep 17 00:00:00 2001 From: Max Knor Date: Tue, 21 Jun 2016 16:48:52 +0200 Subject: [PATCH 4/4] Update start.ps1 --- .../mssql-server-2014-express-windows/dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile b/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile index e76236a8..2c3d1655 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile @@ -28,4 +28,4 @@ RUN powershell -Command \ set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433 ; \ set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ; -CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\" \ No newline at end of file +CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\" -Verbose \ No newline at end of file