Skip to content

Instantly share code, notes, and snippets.

@wsmelton
Last active July 10, 2020 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wsmelton/fd8123eadef0b74b9ae2895fb0f51960 to your computer and use it in GitHub Desktop.
Save wsmelton/fd8123eadef0b74b9ae2895fb0f51960 to your computer and use it in GitHub Desktop.
Functions to dynamically create. Add to your profile and just have the latest release of Docker for Windows installed.
function New-SqlContainer {
<#
.SYNOPSIS
Use to create n+ number of containers for development purposes.
.PARAMETER Number
Provide the number of containers you want created, minimum of 1.
.PARAMETER Prefix
Provide the prefix to use in the contain names (e.g. kringer1, kringer2, etc)
.PARAMETER Version
Version of SQL Server, based on the images you have pulled down (e.g. 2017, 2019)
#>
param(
[int]$Number,
[string]$Prefix = 'kringer',
[int]$Version = 2019,
[switch]$OutputOnly
)
$script:currentDockerComposeFile = "$env:TEMP\docker-compose$(Get-Date -Format FileDateTime).yml"
if (Test-Path $currentDockerComposeFile) {
Remove-Item $currentDockerComposeFile -Force
}
if ($OutputOnly) {
"version: '3.7'`nservices:"
} else {
"version: '3.7'`nservices:" | Out-File $currentDockerComposeFile
}
for ($n=1;$n -le $Number; $n++) {
$containerTemplate = @"
$($Prefix)$($n):
container_name: '$($Prefix)$($n)'
image:
mcr.microsoft.com/mssql/server:$($Version)-latest
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=password
ports:
- '141$($n.ToString()):1433'
volumes:
- c:\container\$($Prefix)$($n)\sqlsystem:/var/opt/mssql/data
- c:\container\$($Prefix)$($n)\sqluser:/var/opt/mssql/sqlserver
- c:\container\$($Prefix)$($n)\sqlbackups:/var/opt/mssql/backups
"@
if ($OutputOnly) {
$containerTemplate
} else {
$containerTemplate | Out-File $currentDockerComposeFile -Append
}
}
if (-not $OutputOnly) {
docker-compose.exe -f $currentDockerComposeFile up -d --build
}
}
function Remove-SqlContainer {
if (-not (Test-Path $currentDockerComposeFile)) {
Write-Warning "docker-compose file not found: $currentDockerComposeFile"
continue
}
docker-compose.exe -f $currentDockerComposeFile down
if (Test-Path $currentDockerComposeFile) {
Remove-Item $currentDockerComposeFile -Force -Confirm:$false -ErrorAction SilentlyContinue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment