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