info@techdevops.com | 437-991-3573 | Data Engineering Services
TechDevOps.com
Resources Tools
Experts in Microsoft SQL Server on Windows, Linux, Containers | Clusters, Always On, FCI | Migrations, Cloud, Performance



Using PowerShell to spin up Windock's Docker Containers running SQL Server with Cloned Databases
by BF (Principal Consultant; Architecture; Engineering)
2018-02-24









Windocks.com delivers cloned relational database environments for support of software development, test, and for reporting and BI use. Database clones are complete environments, supporting full read/write operations that are delivered in seconds and consume minimal storage resources. Containers and clones are also proving ideal for use with Jenkins, Team City and other DevOps Continuous Integration strategies.




Use Powershell to create an Image & Container w/ Cloned SQL Server Databases:





# This Powershell script builds a SQL Server container from a dockerfile
param(
[String]$directoryPath="c:\ops\testFastCloneFromRawBackup\",
[String]$imagename="opscustomers06powershell"
)
Set-ExecutionPolicy -ExecutionPolicy Bypass

Write-Host " "
Write-Host "Build Image - Starting."
Write-Host " "

$binDir = $SystemDrive + "\windocks\client"
$dockerExe = $binDir + "\docker.exe"
#$buildCommand = $dockerExe + " build -t " + $directoryPath + " > .\buildoutput.txt"
$buildCommand = $dockerExe + " build -t " + $imagename + " " + $directoryPath + " > C:\ops\testFastCloneFromRawBackup\buildoutputImage.txt"

iex $buildCommand

Write-Host "Build Image - Completed."
Write-Host " "

Write-Host "Create Container - Starting."
Write-Host " "

$stopWatch = [System.Diagnostics.Stopwatch]::StartNew()

$buildCommand = $dockerExe + " run -d " + $imagename + " > C:\ops\testFastCloneFromRawBackup\buildoutputContainer.txt"

iex $buildCommand

#$path = $PSScriptRoot + "\buildoutput.txt"
$path = $directoryPath + "buildoutputContainer.txt"

$content = [System.IO.File]::ReadAllText($path)
$separator1 = "&"

$a, $b, $c, $d = $content.Split($separator1,4)


$separator2 = "="
$idText, $containerIdUntrimmed = $a.Split($separator2, 2)
#$containerIdUntrimmed
$containerId = $containerIdUntrimmed.Trim()

$portText, $containerPortUntrimmed = $b.Split($separator2, 2)
$containerPort = $containerPortUntrimmed.Trim()

#Write-Host "Container ID: " $containerId
Write-Host "Create Container - Completed."
Write-Host " "


Write-Host "Starting Container."
Write-Host " "

$buildCommand = $dockerExe + " start " + $containerId + " > C:\ops\testFastCloneFromRawBackup\buildoutput1.txt"

iex $buildCommand

Write-Host "Container is started : ID: " $containerId " on port: " $containerPort
Write-Host " "

$timeTaken = $stopWatch.ElapsedMilliseconds / 1000
$timeTakenTruncated = [math]::truncate($timeTaken)

Write-Host "Total time taken: " $timeTakenTruncated " seconds "
Write-Host " "


Powershell output messages:




Access Cloned SQL Databases in a fresh SQL Instance: