Skip to content

Commit 5831dcc

Browse files
committed
Merge remote-tracking branch 'refs/remotes/Microsoft/master' into json
2 parents 0978621 + f93e4bc commit 5831dcc

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# mssql server 2014 express image listening on static port 1433
2-
#
3-
# Note: This dockerfile is based on Buc Rogers' work here:
2+
#
3+
# Note: This dockerfile is based on Buc Rogers' work here:
44
# https://github.com/brogersyh/Dockerfiles-for-windows/tree/master/sqlexpress
55
#
66
# .NET 3.5 required for SQL Server
@@ -12,19 +12,20 @@ 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 . /
1819
WORKDIR /
1920

20-
# download and install Microsoft SQL 2014 Express Edition in one step
21+
# download and install Microsoft SQL 2014 Express Edition in one step
2122
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
2223

2324
RUN powershell -Command \
2425
set-strictmode -version latest ; \
2526
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 ; \
28-
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql12.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ;
29+
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%\" -Verbose

samples/manage/windows-containers/mssql-server-2014-express-windows/readme.md

+33-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Note: This dockerfile is based on Buc Rogers' work that can be found [here] (htt
1717
## About this sample
1818

1919
1. **Applies to:** SQL Server 2014 Express, Windows Server Technical Preview 4 or later
20-
5. **Authors:** Perry Skountrianos [perrysk-msft]
20+
5. **Authors:** Perry Skountrianos [perrysk-msft], Max Knor [knom]
2121

2222
<a name=before-you-begin></a>
2323

@@ -27,16 +27,44 @@ To run this sample, you need the following prerequisites.
2727

2828
**Software prerequisites:**
2929

30-
You can run the container with the following command. Note the you'll need Windows Server Core TP5 v10.0.14300.1000.
31-
docker run -p 1433:1433 --env sa_password=<YOUR SA PASSWORD> microsoft/mssql-server-2014-express-windows
30+
You can run the container with the following command.
31+
(Note the you'll need Windows Server Core TP5 v10.0.14300.1000.)
32+
33+
````
34+
docker run -p 1433:1433 -v C:/temp/:C:/temp/ --env sa_password=<YOUR SA PASSWORD> --env attach_dbs="<DB-JSON-CONFIG>" -v microsoft/mssql-server-2014-express-windows
35+
````
36+
37+
- **-p HostPort:containerPort** is for port-mapping a container network port to a host port.
38+
- **-v HostPath:containerPath** is for mounting a folder from the host inside the container.
39+
40+
This can be used for saving database outside of the container.
3241

3342
<a name=run-this-sample></a>
3443

3544
## Run this sample
3645

37-
The image provides one environment variable to set the sa password: </br>
46+
The image provides two environment variables to optionally set: </br>
3847
- sa_password: Sets the sa password and enables the sa login
39-
48+
- attach_dbs: The configuration for attaching custom DBs (.mdf, .ldf files).
49+
50+
This should be a JSON string, formed like the following (note the SINGLE quotes and everything in one line):
51+
```
52+
[{'dbName':'MaxDb','dbFiles':['C:\\temp\\maxtest.mdf','C:\\temp\\maxtest_log.
53+
ldf']},{'dbName':'PerryDb','dbFiles':['C:\\temp\\perrytest.mdf','C:\\temp\\perrytest_log.
54+
ldf']}]
55+
```
56+
There can be zero to many databases in the array.
57+
- dbName: The name of the database
58+
- dbFiles: An array of absolute paths to the .MDF and .LDF files.
59+
60+
Can be one or many, note that the path has double backslashes for escaping!
61+
62+
This example shows all parameters in action:
63+
```
64+
docker run -p 1433:1433 -v C:/temp/:C:/temp/ --env attach_dbs="[{'dbName':'MaxTest','dbFiles':['C:\\temp\\maxtest.mdf','C:\\temp\\maxtest_log.
65+
ldf']}]" microsoft/mssql-server-2014-express-windows
66+
```
67+
4068
<a name=sample-details></a>
4169

4270
## Sample details
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)