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 da4c84c1..323db222 100644 --- a/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile +++ b/samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile @@ -1,6 +1,6 @@ # mssql server 2014 express image listening on static port 1433 -# -# Note: This dockerfile is based on Buc Rogers' work here: +# +# Note: This dockerfile is based on Buc Rogers' work here: # https://github.com/brogersyh/Dockerfiles-for-windows/tree/master/sqlexpress # # .NET 3.5 required for SQL Server @@ -12,12 +12,13 @@ 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 . / WORKDIR / -# download and install Microsoft SQL 2014 Express Edition in one step +# download and install Microsoft SQL 2014 Express Edition in one step RUN powershell -Command (New-Object System.Net.WebClient).DownloadFile('%sql_express_download_url%', 'sqlexpress.exe') && /sqlexpress.exe /qs /x:setup && /setup/setup.exe /q /ACTION=Install /INSTANCENAME=SQLEXPRESS /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT="NT AUTHORITY\System" /SQLSYSADMINACCOUNTS="BUILTIN\ADMINISTRATORS" /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS && del /F /Q sqlexpress.exe && rd /q /s setup RUN powershell -Command \ @@ -25,6 +26,6 @@ RUN powershell -Command \ 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 ; + set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ; -CMD powershell ./start %sa_password% +CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\" -Verbose 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..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 @@ -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']}]" microsoft/mssql-server-2014-express-windows +``` + ## Sample details 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