Skip to content

Instantly share code, notes, and snippets.

@aadennis
Last active September 27, 2020 15:44
Show Gist options
  • Save aadennis/68edc2be35673998b5f46ec528f50748 to your computer and use it in GitHub Desktop.
Save aadennis/68edc2be35673998b5f46ec528f50748 to your computer and use it in GitHub Desktop.
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. $here/Utilities.ps1
#. "C:\Sandbox\docker\utilities\DockerUtilities.ps1"
function Remove-AllContainers {
$containers = $(docker ps -a) | Select-Object -Skip 1
$containers | ForEach-Object {
$containerId = $_.substring(0, 12)
Remove-Container $containerId
}
}
function Remove-Container($container) {
docker ps -a
docker kill $container
docker rm $container
}
function Remove-AllUntaggedImages {
docker images
write-host "Running..."
$untagged = "<none> <none>"
$untagged2 = "<none> <none>" # how can there be 2 versions?!
$images = $(docker images) | Select-Object -Skip 1
$images | ForEach-Object {
$image = $_
if (($image.substring(0, 49) -eq $untagged)) {
Write-Host "Got one - display format 1"
$imageId = $image.substring(72, 12)
docker rmi $imageId
}
if (($image.substring(0, 45) -eq $untagged2)) {
Write-Host "Got one - display format 2"
$imageId = $image.substring(59, 12)
docker rmi $imageId
}
}
docker images
}
#Remove-ImageSet 142,88f
function Remove-ImageSet($imageIdSet) {
$imageIdSet | % {
docker rmi $_
}
docker images
}
function Remove-Image($image) {
docker images
docker rmi $image
}
function Build-Dockerfile($tag = "densql:2", $containerPort = "8082") {
docker image build --build-arg ENV_NAME=TEST --tag $tag --file ./Dockerfile .
docker container run --detach --publish $containerPort:80 $tag
}
# aliases
function psa { # Docker Psa -A
docker ps -a
}
function di { # Docker Images
docker images
}
function rmi($image) { # Docker RmI (ImageId)
docker images
docker rmi $image
}
function dins($ImageId) { # Docker Inspect (ImageId)
Write-Host "DOCKER INSPECT (IMAGEID)"
docker images
docker inspect $ImageId
}
function dcl($ContainerId) { # Docker Container Logs (ContainerId)
psa
docker container logs $ContainerId
}
function dn { # Docker Networks ls
Write-Host "DOCKER NETWORKS LS"
docker network ls
}
function dh($ImageId) { # Docker History (ImageId)
# https://docs.docker.com/engine/reference/commandline/history/
Write-Host "DOCKER HISTORY (IMAGEID)"
docker images
docker history $ImageId
docker history --no-trunc --format "{{.ID}} {{.CreatedBy}}" $ImageId
}
function Run-Container($volumeShare, $image) { # Run 1 of the listed containers
$imageSet =
"mcr.microsoft.com/windows/servercore:ltsc2016",
"mcr.microsoft.com/windows/servercore:ltsc2019",
"mcr.microsoft.com/windows/servercore:10.0.14393.3808",
"nf48ws16sql2019:sqlfilesonly",
"nf48ws16sql2017:sqlfilesonly",
"nf48ws16sql2017:sqlinstalled"
$volumeShare = "c:\HostShare:c:\ContainerShare"
if ($image -eq $null) {
Write-Host "No image passed in. Select from the following..."
$index = 0
$imageSet | ForEach-Object {
$currentImage = $_
$index++
Write-Host "$($index): $currentImage"
}
$imageIndexToUse = Read-Host "Enter a number [1] to [$index]"
if ($imageIndexToUse -lt 1 -or $imageIndexToUse -gt $index) {
write-host "$imageIndexToUse is not a valid image index. Exiting..."
return
}
$image = $imageSet[$imageIndexToUse - 1]
}
# Possibly redundantly on subsequent executions,
# a) create the folder structure so that the container can also use
# utilitiies (e.g. Get-WindowsInfo)
# b) copy those utilities into that folder - some of those will be
# relevant to the container, and some will not. That doesn't matter.
$hostShare = "C:\HostShare\"
if (!(Test-Path "$hostShare\logs")) { New-Item -Path "$hostShare\logs" -ItemType Directory }
if (!(Test-Path "$hostShare\scripts")) {New-Item -Path "$hostShare\scripts" -ItemType Directory }
if (!(Test-Path "$hostShare\files")) {New-Item -Path "$hostShare\files" -ItemType Directory } # general folder for swapping files
"DockerUtilities.ps1", "Utilities.ps1" | ForEach-Object {
Copy-Item "$PSScriptRoot/$_" "$hostShare\scripts\$_"
}
Write-Output ". c:\containershare\scripts\DockerUtilities.ps1" > "$hostShare\readme.txt"
Write-Host "running container..."
Write-Host "volumeshare: $volumeShare"
Write-Host "image: $image"
docker run -v $volumeShare -it $image powershell
}
<#
.SYNOPSIS
Get a named set of images from the Hub
.EXAMPLE
Get-ImageSetFromHub()
.INPUTS
Inputs (if any)
.OUTPUTS
Output (if any)
.NOTES
General notes
#>
function Get-ImageSetFromHub() { # Get a named set of images from the Hub
# https://docs.microsoft.com/en-us/virtualization/windowscontainers/manage-containers/container-base-images
# https://hub.docker.com/_/microsoft-windows-nanoserver
$imageSet =
"mcr.microsoft.com/windows:2004",
"mcr.microsoft.com/windows/nanoserver:2004", # 1 layer - 100MB
"mcr.microsoft.com/windows/nanoserver:10.0.19041.508", # 1 layer - 100MB
"mcr.microsoft.com/windows/servercore:ltsc2016",
"mcr.microsoft.com/windows/servercore:ltsc2019",
"mcr.microsoft.com/windows/servercore:10.0.14393.3808",
"mcr.microsoft.com/dotnet/framework/runtime:4.8",
"mcr.microsoft.com/dotnet/framework/sdk:4.8"
if ($imageSet -eq $null) {
Write-Host "No image set passed in. Select from the following.... Exiting..."
throw
}
$imageSet | ForEach-Object {
$currentImage = $_
Write-Host "Pulling [$currentImage] from Docker Hub" -BackgroundColor Yellow -ForegroundColor Red
docker pull $currentImage
}
Docker images
}
function Get-WindowsInfo() { # Get Windows info (context can be host or container)
Write-Host "Hostname: $(& HOSTNAME.EXE)"
systeminfo /fo csv | ConvertFrom-Csv | Select-Object OS*, System*, Hotfix* | Format-List
write-host "Semantic file version..."
(Get-ItemProperty -Path C:\Windows\System32\hal.dll).VersionInfo.FileVersion
Get-DotNetVersionsFromRegistry
# Write-host "OS Caption..." - redundant
# (Get-WmiObject -Class win32_OperatingSystem).Caption
}
function Get-DotNetVersionsFromRegistry() {
# https://stackoverflow.com/questions/3487265/powershell-script-to-return-versions-of-net-framework-on-a-machine
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
Get-ItemProperty -name Version, Release -EA 0 |
Where { $_.PSChildName -match '^(?![SW])\p{L}'} |
Select PSChildName, Version, Release, @{
name = "Product"
expression = {
switch -regex ($_.Release) {
"378389" { [Version]"4.5" }
"378675|378758" { [Version]"4.5.1" }
"379893" { [Version]"4.5.2" }
"393295|393297" { [Version]"4.6" }
"394254|394271" { [Version]"4.6.1" }
"394802|394806" { [Version]"4.6.2" }
"460798|460805" { [Version]"4.7" }
"461308|461310" { [Version]"4.7.1" }
"461808|461814" { [Version]"4.7.2" }
{$_ -gt 461814} { [Version]"Undocumented version (> 4.7.2), please update script" }
}
}
}
}
Get-DotNetVersionsFromRegistry
Get-FunctionsInScript $here/DockerUtilities.ps1
Get-FunctionsInScript $here/Utilities.ps1
function Build-Dockerfile($tag = "denstatic:2", $containerPort = "8081") {
docker image build --build-arg ENV_NAME=TEST --tag $tag .
docker container run --detach --publish $containerPort:80 $tag
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment