Skip to content

Commit 2188003

Browse files
author
Max Knor
committed
Added custom DB ATTACH support to the start.ps1 as well as dockerfile.
Changes to be committed: modified: dockerfile modified: start.ps1
1 parent e742ec8 commit 2188003

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

samples/manage/windows-containers/mssql-server-2014-express-windows/dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ MAINTAINER Perry Skountrianos
1212
# set environment variables
1313
ENV sql_express_download_url "https://download.microsoft.com/download/1/5/6/156992E6-F7C7-4E55-833D-249BD2348138/ENU/x64/SQLEXPR_x64_ENU.exe"
1414
ENV sa_password _
15+
ENV attach_dbs "[]"
1516

1617
# make install files accessible
1718
COPY . /
@@ -22,9 +23,9 @@ RUN powershell -Command (New-Object System.Net.WebClient).DownloadFile('%sql_exp
2223

2324
RUN powershell -Command \
2425
set-strictmode -version latest ; \
25-
stop-service MSSQL`$%sqlinstance% ; \
26+
stop-service MSSQL`$SQLEXPRESS ; \
2627
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpdynamicports -value '' ; \
2728
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433 ; \
2829
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ;
2930

30-
CMD powershell ./start %sa_password%
31+
CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\"
Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
1-
# The script sets the sa password and start the SQl Service
1+
# The script sets the sa password and start the SQL Service
2+
# Also it attaches additional database from the disk
3+
# The format for attach_dbs
24

35
param(
46
[Parameter(Mandatory=$false)]
5-
[string]$sa_password
7+
[string]$sa_password,
8+
9+
[Parameter(Mandatory=$false)]
10+
[string]$attach_dbs
611
)
712

813
# start the service
14+
Write-Verbose "Starting SQL Server"
915
start-service MSSQL`$SQLEXPRESS
1016

11-
1217
if($sa_password -ne "_"){
18+
Write-Verbose "Changing SA login credentials"
1319
$sqlcmd = "ALTER LOGIN sa with password=" +"'" + $sa_password + "'" + ";ALTER LOGIN sa ENABLE;"
1420
Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLEXPRESS"
1521
}
1622

23+
$attach_dbs = $attach_dbs | ConvertFrom-Json
24+
25+
if ($null -ne $attach_dbs){
26+
Write-Verbose "Attaching database(s)"
27+
Foreach($db in $attach_dbs)
28+
{
29+
$files = @();
30+
Foreach($file in $db.dbFiles)
31+
{
32+
$files += "(FILENAME = 'N$($file)')";
33+
}
34+
35+
$files = $files -join ","
36+
$sqlcmd = "sp_detach_db $($db.dbName);GO;CREATE DATABASE $($db.dbName) ON $($files) FOR ATTACH ;GO;"
37+
38+
Write-Host Invoke-Sqlcmd -Query $($sqlcmd) -ServerInstance ".\SQLEXPRESS"
39+
Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLEXPRESS"
40+
}
41+
}
42+
1743
while ($true) { Start-Sleep -Seconds 3600 }

0 commit comments

Comments
 (0)