SQL Server 2016 in Win Containers

This commit is contained in:
Perry Skountrianos - MSFT
2016-09-29 12:18:56 -07:00
parent 33ca7fa4db
commit d9254d0d6c
6 changed files with 364 additions and 0 deletions
@@ -0,0 +1,32 @@
# mssql server 2016 developer image
FROM microsoft/windowsservercore
# maintainer for image metadata
MAINTAINER Perry Skountrianos
# set environment variables
ENV sql_server_exe_download_url "https://go.microsoft.com/fwlink/?linkid=829177"
ENV sql_server_box_download_url "https://go.microsoft.com/fwlink/?linkid=829178"
ENV sa_password _
ENV attach_dbs "[]"
# make install files accessible
COPY . /
WORKDIR /
# download sql server box
RUN powershell -Command (New-Object System.Net.WebClient).DownloadFile('%sql_server_box_download_url%', 'SQLServer2016-DEV-x64-ENU.box')
# download and install Microsoft SQL 2014 Express Edition in one step
RUN powershell -Command (New-Object System.Net.WebClient).DownloadFile('%sql_server_exe_download_url%', 'SQLServer2016-DEV-x64-ENU.exe') && /SQLServer2016-DEV-x64-ENU.exe /qs /x:setup && /setup/setup.exe /q /ACTION=Install /INSTANCENAME=SQLDEV /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT="NT AUTHORITY\System" /SQLSYSADMINACCOUNTS="BUILTIN\ADMINISTRATORS" /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS && del /F /Q SQLServer2016-DEV-x64-ENU.exe && del /F /Q SQLServer2016-DEV-x64-ENU.box && rd /q /s setup
RUN powershell -Command \
set-strictmode -version latest ; \
stop-service MSSQL`$SQLDEV ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql13.SQLDEV\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpdynamicports -value '' ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql13.SQLDEV\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433 ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql13.SQLDEV\mssqlserver\' -name LoginMode -value 2 ;
CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\" -Verbose
@@ -0,0 +1,103 @@
# mssql-server-2016-express-windows
This Dockerfile helps developers to get started using SQL Server 2016 Developer in Windows Server Core Containers. The file downloads and installs SQL Server 2016 Developer with the default setup parameters.
### Contents
[About this sample](#about-this-sample)<br/>
[Before you begin](#before-you-begin)<br/>
[Run this sample](#run-this-sample)<br/>
[Sample details](#sample-details)<br/>
[Disclaimers](#disclaimers)<br/>
[Related links](#related-links)<br/>
<a name=about-this-sample></a>
## About this sample
1. **Applies to:** SQL Server 2016 Developer, Windows Server 2016
5. **Authors:** Perry Skountrianos [perrysk-msft]
<a name=before-you-begin></a>
## Before you begin
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 2016)
````
docker run -d -p 1433:1433 -v C:/temp/:C:/temp/ --env sa_password=<YOUR SA PASSWORD> --env attach_dbs="<DB-JSON-CONFIG>" microsoft/mssql-server-2016-developer-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.
- **-it** can be used to show the verbose output of the SQL startup script.
Use this to debug the container in case of issues.
<a name=run-this-sample></a>
## Run this sample
The image provides two environment variables to optionally set: </br>
- **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, in the following format (note the use of SINGLE quotes!)
```
[
{
'dbName': 'MaxDb',
'dbFiles': ['C:\\temp\\maxtest.mdf',
'C:\\temp\\maxtest_log.ldf']
},
{
'dbName': 'PerryDb',
'dbFiles': ['C:\\temp\\perrytest.mdf',
'C:\\temp\\perrytest_log.ldf']
}
]
```
This is an array of databases, which can have zero to N databases.
Each consisting of:
- **dbName**: The name of the database
- **dbFiles**: An array of one or many absolute paths to the .MDF and .LDF files.
**Note:**
The path has double backslashes for escaping!
The path refers to files **within the container**. So make sure to include them in the image or mount them via **-v**!
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-2016-developer-windows
```
<a name=sample-details></a>
## Sample details
<a name=disclaimers></a>
## Disclaimers
The code included in this sample is not intended to be a set of best practices on how to build scalable enterprise grade applications. This is beyond the scope of this quick start sample.
<a name=related-links></a>
## Related Links
<!-- Links to more articles. Remember to delete "en-us" from the link path. -->
For more information, see these articles:
- [Windows Containers] (https://msdn.microsoft.com/en-us/virtualization/windowscontainers/about/about_overview)
- [Windows-based containers: Modern app development with enterprise-grade control] (https://www.youtube.com/watch?v=Ryx3o0rD5lY&feature=youtu.be)
- [Windows Containers: What, Why and How] (https://channel9.msdn.com/Events/Build/2015/2-704)
- [SQL Server in Windows Containers] (https://blogs.msdn.microsoft.com/sqlserverstorageengine/2016/03/21/sql-server-in-windows-containers/#comments)
@@ -0,0 +1,46 @@
# 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,
[Parameter(Mandatory=$false)]
[string]$attach_dbs
)
# start the service
Write-Verbose "Starting SQL Server"
start-service MSSQL`$SQLDEV
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 ".\SQLDEV"
}
$attach_dbs_cleaned = $attach_dbs.TrimStart('\\').TrimEnd('\\')
$dbs = $attach_dbs_cleaned | ConvertFrom-Json
if ($null -ne $dbs -And $dbs.Length -gt 0){
Write-Verbose "Attaching $($dbs.Length) database(s)"
Foreach($db in $dbs)
{
$files = @();
Foreach($file in $db.dbFiles)
{
$files += "(FILENAME = N'$($file)')";
}
$files = $files -join ","
$sqlcmd = "sp_detach_db $($db.dbName);CREATE DATABASE $($db.dbName) ON $($files) FOR ATTACH ;"
Write-Verbose "Invoke-Sqlcmd -Query $($sqlcmd) -ServerInstance '.\SQLDEV'"
Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLDEV"
}
}
Write-Verbose "Started SQL Server."
while ($true) { Start-Sleep -Seconds 3600 }
@@ -0,0 +1,28 @@
# mssql server 2016 express image
#
FROM microsoft/windowsservercore
# maintainer for image metadata
MAINTAINER Perry Skountrianos
# set environment variables
ENV sql_express_download_url "https://go.microsoft.com/fwlink/?linkid=829176"
ENV sa_password _
ENV attach_dbs "[]"
# make install files accessible
COPY . /
WORKDIR /
# 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 \
set-strictmode -version latest ; \
stop-service MSSQL`$SQLEXPRESS ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql13.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpdynamicports -value '' ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql13.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' -name tcpport -value 1433 ; \
set-itemproperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql13.SQLEXPRESS\mssqlserver\' -name LoginMode -value 2 ;
CMD powershell ./start -sa_password %sa_password% -attach_dbs \"%attach_dbs%\" -Verbose
@@ -0,0 +1,109 @@
# mssql-server-2016-express-windows
This Dockerfile helps developers to get started using SQL Server 2016 Express in Windows Server Core Containers. The file downloads and installs SQL Server 2016 Express with the default setup parameters.
### Contents
[About this sample](#about-this-sample)<br/>
[Before you begin](#before-you-begin)<br/>
[Run this sample](#run-this-sample)<br/>
[Sample details](#sample-details)<br/>
[Disclaimers](#disclaimers)<br/>
[Related links](#related-links)<br/>
<a name=about-this-sample></a>
## About this sample
1. **Applies to:** SQL Server 2016 Express, Windows Server 2016
5. **Authors:** Perry Skountrianos [perrysk-msft]
<a name=before-you-begin></a>
## Before you begin
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 2016)
````
docker run -d -p 1433:1433 -v C:/temp/:C:/temp/ --env sa_password=<YOUR SA PASSWORD> --env attach_dbs="<DB-JSON-CONFIG>" microsoft/mssql-server-2016-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.
- **-it** can be used to show the verbose output of the SQL startup script.
Use this to debug the container in case of issues.
<a name=run-this-sample></a>
## Run this sample
The image provides two environment variables to optionally set: </br>
- **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, in the following format (note the use of SINGLE quotes!)
```
[
{
'dbName': 'MaxDb',
'dbFiles': ['C:\\temp\\maxtest.mdf',
'C:\\temp\\maxtest_log.ldf']
},
{
'dbName': 'PerryDb',
'dbFiles': ['C:\\temp\\perrytest.mdf',
'C:\\temp\\perrytest_log.ldf']
}
]
```
This is an array of databases, which can have zero to N databases.
Each consisting of:
- **dbName**: The name of the database
- **dbFiles**: An array of one or many absolute paths to the .MDF and .LDF files.
**Note:**
The path has double backslashes for escaping!
The path refers to files **within the container**. So make sure to include them in the image or mount them via **-v**!
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-2016-express-windows
```
<a name=sample-details></a>
## Sample details
The Dockerfile downloads and installs SQL Server 2016 Express with the following default setup parameters that could be changed (if needed) after the image is installed.
- Collation: SQL_Latin1_General_CP1_CI_AS
- SQL Instance Name: SQLEXPRESS
- Root Directory: C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL
- Language: English (United Stated)
<a name=disclaimers></a>
## Disclaimers
The code included in this sample is not intended to be a set of best practices on how to build scalable enterprise grade applications. This is beyond the scope of this quick start sample.
<a name=related-links></a>
## Related Links
<!-- Links to more articles. Remember to delete "en-us" from the link path. -->
For more information, see these articles:
- [Windows Containers] (https://msdn.microsoft.com/en-us/virtualization/windowscontainers/about/about_overview)
- [Windows-based containers: Modern app development with enterprise-grade control] (https://www.youtube.com/watch?v=Ryx3o0rD5lY&feature=youtu.be)
- [Windows Containers: What, Why and How] (https://channel9.msdn.com/Events/Build/2015/2-704)
- [SQL Server in Windows Containers] (https://blogs.msdn.microsoft.com/sqlserverstorageengine/2016/03/21/sql-server-in-windows-containers/#comments)
@@ -0,0 +1,46 @@
# 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,
[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_cleaned = $attach_dbs.TrimStart('\\').TrimEnd('\\')
$dbs = $attach_dbs_cleaned | ConvertFrom-Json
if ($null -ne $dbs -And $dbs.Length -gt 0){
Write-Verbose "Attaching $($dbs.Length) database(s)"
Foreach($db in $dbs)
{
$files = @();
Foreach($file in $db.dbFiles)
{
$files += "(FILENAME = N'$($file)')";
}
$files = $files -join ","
$sqlcmd = "sp_detach_db $($db.dbName);CREATE DATABASE $($db.dbName) ON $($files) FOR ATTACH ;"
Write-Verbose "Invoke-Sqlcmd -Query $($sqlcmd) -ServerInstance '.\SQLEXPRESS'"
Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLEXPRESS"
}
}
Write-Verbose "Started SQL Server."
while ($true) { Start-Sleep -Seconds 3600 }