From 0e93cd15c9e5d08fab89f1ed9df142933b191040 Mon Sep 17 00:00:00 2001 From: "brasmith@microsoft.com" Date: Thu, 9 Mar 2023 13:54:51 -0800 Subject: [PATCH 1/4] Added logic to download and extract docker from download.docker.com. Also updated the readme. --- helpful_tools/Install-DockerCE/README.md | 92 +++++++++++-------- .../Install-DockerCE/install-docker-ce.ps1 | 82 +++++++++++++---- 2 files changed, 116 insertions(+), 58 deletions(-) diff --git a/helpful_tools/Install-DockerCE/README.md b/helpful_tools/Install-DockerCE/README.md index bc81424..6955ad2 100644 --- a/helpful_tools/Install-DockerCE/README.md +++ b/helpful_tools/Install-DockerCE/README.md @@ -1,23 +1,15 @@ -## Install-ContainerHost.ps1 +## install-docker-ce.ps1 #### NAME - Install-ContainerHost.ps1 + install-docker-ce.ps1 #### SYNOPSIS - Installs the prerequisites for creating Windows containers + Installs the prerequisites for running Windows containers with docker CE #### SYNTAX - Install-ContainerHost.ps1 [-DockerPath ] [-ExternalNetAdapter ] - [-Force] [-HyperV] [-NoRestart] [-PSDirect] [-SkipImageImport] - [-UseDHCP] [-WimPath ] [] - - Install-ContainerHost.ps1 [-DockerPath ] [-ExternalNetAdapter ] - [-Force] [-HyperV] [-NoRestart] [-PSDirect] - [-SkipImageImport] [-UseDHCP] [-WimPath ] [] - - Install-ContainerHost.ps1 [-DockerPath ] [-ExternalNetAdapter ] - [-Force] [-HyperV] [-NoRestart] [-PSDirect] - [-SkipImageImport] -Staging [-UseDHCP] [-WimPath ] [] + install-docker-ce.ps1 [-DockerPath ] [-DockerDPath ] [-DockerVersion ] [-ContainerBaseImage ] [-ExternalNetAdapter ] + [-Force] [-HyperV] [-SkipDefaultHost] [-NATSubnet ] [-NoRestart] [-PSDirect] [-Staging] + [-UseDHCP] [-WimPath ] [-TarPath] [] #### DESCRIPTION @@ -25,23 +17,50 @@ #### PARAMETERS - -DockerPath - Path to Docker.exe, can be local or URI + -DockerPath [] + Path to Docker.exe, can be local or URI. - Required? false + Required? True Position? named - Default value https://aka.ms/tp4/docker + Default value default + Accept pipeline input? false + Accept wildcard characters? false + + -DockerDPath [] + Path to DockerD.exe, can be local or URI. + + Required? True + Position? named + Default value default + Accept pipeline input? false + Accept wildcard characters? false + + -DockerVersion [] + The version of docker to use. + + Required? True + Position? named + Default value latest Accept pipeline input? false Accept wildcard characters? false - -ExternalNetAdapter - Specify a specific network adapter to bind to a DHCP switch + -ExternalNetAdapter [] + Specify a specific network adapter to bind to a DHCP switch. Required? false Position? named Default value Accept pipeline input? false Accept wildcard characters? false + + -SkipDefaultHost [] + Prevents setting localhost as the default network configuration. + + Required? false + Position? named + Default value False + Accept pipeline input? false + Accept wildcard characters? false -Force [] If a restart is required, forces an immediate restart. @@ -60,6 +79,15 @@ Default value False Accept pipeline input? false Accept wildcard characters? false + + -NATSubnet [] + Use to override the default Docker NAT Subnet when in NAT mode. + + Required? false + Position? named + Default value + Accept pipeline input? false + Accept wildcard characters? false -NoRestart [] If a restart is required the script will terminate and will not reboot the machine @@ -70,20 +98,12 @@ Accept pipeline input? false Accept wildcard characters? false - -PSDirect [] + -ContainerBaseImage [] + Use this to specify the URI of the container base image you wish to pull Required? false Position? named - Default value False - Accept pipeline input? false - Accept wildcard characters? false - - -SkipImageImport [] - Skips import of the base WindowsServerCore image. - - Required? false - Position? named - Default value False + Default value Accept pipeline input? false Accept wildcard characters? false @@ -103,8 +123,8 @@ Accept pipeline input? false Accept wildcard characters? false - -WimPath - Path to .wim file that contains the base package image + -TarPath + Path to the .tar that is the base image to load into Docker. Required? false Position? named @@ -112,12 +132,6 @@ Accept pipeline input? false Accept wildcard characters? false - - This cmdlet supports the common parameters: Verbose, Debug, - ErrorAction, ErrorVariable, WarningAction, WarningVariable, - OutBuffer, PipelineVariable, and OutVariable. For more information, see - about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). - #### NOTES Copyright (c) Microsoft Corporation. All rights reserved. diff --git a/helpful_tools/Install-DockerCE/install-docker-ce.ps1 b/helpful_tools/Install-DockerCE/install-docker-ce.ps1 index 7760c57..5f01843 100644 --- a/helpful_tools/Install-DockerCE/install-docker-ce.ps1 +++ b/helpful_tools/Install-DockerCE/install-docker-ce.ps1 @@ -27,11 +27,14 @@ .PARAMETER DockerDPath Path to DockerD.exe, can be local or URI + .PARAMETER DockerVersion + Version of docker to pull from download.docker.com - ! OVERRIDDEN BY DockerPath & DockerDPath + .PARAMETER ExternalNetAdapter Specify a specific network adapter to bind to a DHCP network .PARAMETER SkipDefaultHost - Prevents setting localhost as the default n + Prevents setting localhost as the default network configuration .PARAMETER Force If a restart is required, forces an immediate restart. @@ -46,7 +49,9 @@ If a restart is required the script will terminate and will not reboot the machine .PARAMETER ContainerBaseImage - Use this to specifiy the URI of the container base image you wish to pull + Use this to specify the URI of the container base image you wish to pull + + .PARAMETER Staging .PARAMETER TransparentNetwork If passed, use DHCP configuration. Otherwise, will use default docker network (NAT). (alias -UseDHCP) @@ -55,7 +60,7 @@ Path to the .tar that is the base image to load into Docker. .EXAMPLE - .\Install-ContainerHost.ps1 + .\install-docker-ce.ps1 #> #Requires -Version 5.0 @@ -64,11 +69,15 @@ param( [string] [ValidateNotNullOrEmpty()] - $DockerPath = "https://master.dockerproject.org/windows/x86_64/docker.exe", + $DockerPath = "default", [string] [ValidateNotNullOrEmpty()] - $DockerDPath = "https://master.dockerproject.org/windows/x86_64/dockerd.exe", + $DockerDPath = "default", + + [string] + [ValidateNotNullOrEmpty()] + $DockerVersion = "latest", [string] $ExternalNetAdapter, @@ -88,10 +97,6 @@ param( [switch] $NoRestart, - [Parameter(DontShow)] - [switch] - $PSDirect, - [string] $ContainerBaseImage, @@ -109,12 +114,14 @@ param( ) $global:RebootRequired = $false - $global:ErrorFile = "$pwd\Install-ContainerHost.err" - $global:BootstrapTask = "ContainerBootstrap" - $global:HyperVImage = "NanoServer" +$global:AdminPriviledges = $false + +$global:DefaultDockerLocation = "https://download.docker.com/win/static/stable/x86_64/" +$global:DockerDataPath = "$($env:ProgramData)\docker" +$global:DockerServiceName = "docker" function Restart-And-Run() @@ -393,9 +400,7 @@ Install-ContainerHost Remove-Item $global:ErrorFile Write-Output "Script complete!" -}$global:AdminPriviledges = $false -$global:DockerDataPath = "$($env:ProgramData)\docker" -$global:DockerServiceName = "docker" +} function Copy-File @@ -552,11 +557,11 @@ Install-Docker() param( [string] [ValidateNotNullOrEmpty()] - $DockerPath = "https://master.dockerproject.org/windows/x86_64/docker.exe", + $DockerPath = "default", [string] [ValidateNotNullOrEmpty()] - $DockerDPath = "https://master.dockerproject.org/windows/x86_64/dockerd.exe", + $DockerDPath = "default", [string] [ValidateNotNullOrEmpty()] @@ -571,10 +576,49 @@ Install-Docker() Test-Admin - Write-Output "Installing Docker..." + try { + #If one of these are set to default then the whole .zip needs to be downloaded anyways. + Write-Output "DOCKER $DockerPath" + if ($DockerPath -eq "default" -or $DockerDPath -eq "default") { + Write-Output "Checking Docker version" + $version = ((Invoke-WebRequest -Uri $DefaultDockerLocation).Links | Where-Object {$_.href -like "docker*"}).href | Sort-Object -Descending | Select-Object -First 1 | Select-String -Pattern "docker-(\d+\.\d+\.\d+).+" -AllMatches | Select-Object -Expand Matches | %{ $_.Groups[1].Value } + + if($DockerVersion -ne "latest") { + $version = $DockerVersion + } else { + if(!(((Invoke-WebRequest -Uri $global:DefaultDockerLocation).Links | Where-Object {$_.href -like "*$global:DockerVersion*"}).href | Sort-Object -Descending | Select-Object -First 1)) { + Write-Error "Docker version supplied $global:DockerVersion was invalid, using the latest version." + } + } + + $zipUrl = $global:DefaultDockerLocation + "docker-$version.zip" + $destinationFolder = "$env:UserProfile\DockerDownloads" + + if(!(Test-Path "$destinationFolder")) { + md -Path $destinationFolder | Out-Null + } elseif(Test-Path "$destinationFolder\docker-$version") { + Remove-Item -Recurse -Force "$destinationFolder\docker-$version" + } + + Write-Output "Downloading $zipUrl to $destinationFolder\docker-$version.zip" + Copy-File -SourcePath $zipUrl -DestinationPath "$destinationFolder\docker-$version.zip" + Expand-Archive -Path "$destinationFolder\docker-$version.zip" -DestinationPath "$destinationFolder\docker-$version" + + if($DockerPath -eq "default") { + $DockerPath = "$destinationFolder\docker-$version\docker\docker.exe" + } + if($DockerDPath -eq "default") { + $DockerDPath = "$destinationFolder\docker-$version\docker\dockerd.exe" + } + } + } catch { + Write-Error "Failed to download the latest docker version: $_" + } + + Write-Output "Installing Docker... $DockerPath" Copy-File -SourcePath $DockerPath -DestinationPath $env:windir\System32\docker.exe - Write-Output "Installing Docker daemon..." + Write-Output "Installing Docker daemon... $DockerDPath" Copy-File -SourcePath $DockerDPath -DestinationPath $env:windir\System32\dockerd.exe $dockerConfigPath = Join-Path $global:DockerDataPath "config" From af268ccd26b7ffba4aba04a1655dd86efaed7a95 Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Thu, 9 Mar 2023 14:18:33 -0800 Subject: [PATCH 2/4] Update helpful_tools/Install-DockerCE/README.md Co-authored-by: Bjorn Neergaard --- helpful_tools/Install-DockerCE/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpful_tools/Install-DockerCE/README.md b/helpful_tools/Install-DockerCE/README.md index 6955ad2..1511463 100644 --- a/helpful_tools/Install-DockerCE/README.md +++ b/helpful_tools/Install-DockerCE/README.md @@ -4,7 +4,7 @@ install-docker-ce.ps1 #### SYNOPSIS - Installs the prerequisites for running Windows containers with docker CE + Installs the prerequisites for running Windows containers with Docker CE #### SYNTAX install-docker-ce.ps1 [-DockerPath ] [-DockerDPath ] [-DockerVersion ] [-ContainerBaseImage ] [-ExternalNetAdapter ] From 232d62158f18477f44168574ef34343b31a84f2b Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Thu, 9 Mar 2023 14:19:41 -0800 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Bjorn Neergaard --- helpful_tools/Install-DockerCE/README.md | 2 +- helpful_tools/Install-DockerCE/install-docker-ce.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/helpful_tools/Install-DockerCE/README.md b/helpful_tools/Install-DockerCE/README.md index 1511463..5c70362 100644 --- a/helpful_tools/Install-DockerCE/README.md +++ b/helpful_tools/Install-DockerCE/README.md @@ -99,7 +99,7 @@ Accept wildcard characters? false -ContainerBaseImage [] - Use this to specify the URI of the container base image you wish to pull + Use this to specify the URI of the container base image you wish to pre-pull Required? false Position? named diff --git a/helpful_tools/Install-DockerCE/install-docker-ce.ps1 b/helpful_tools/Install-DockerCE/install-docker-ce.ps1 index 5f01843..68a4384 100644 --- a/helpful_tools/Install-DockerCE/install-docker-ce.ps1 +++ b/helpful_tools/Install-DockerCE/install-docker-ce.ps1 @@ -49,7 +49,7 @@ If a restart is required the script will terminate and will not reboot the machine .PARAMETER ContainerBaseImage - Use this to specify the URI of the container base image you wish to pull + Use this to specify the URI of the container base image you wish to pre-pull .PARAMETER Staging From f46f25c303bd0d615b359fb1c50c12fb83ce1dbf Mon Sep 17 00:00:00 2001 From: "brasmith@microsoft.com" Date: Thu, 9 Mar 2023 14:40:49 -0800 Subject: [PATCH 4/4] Condensed the version check logic. --- .../Install-DockerCE/install-docker-ce.ps1 | 69 ++++++++++--------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/helpful_tools/Install-DockerCE/install-docker-ce.ps1 b/helpful_tools/Install-DockerCE/install-docker-ce.ps1 index 5f01843..415b942 100644 --- a/helpful_tools/Install-DockerCE/install-docker-ce.ps1 +++ b/helpful_tools/Install-DockerCE/install-docker-ce.ps1 @@ -576,43 +576,44 @@ Install-Docker() Test-Admin - try { - #If one of these are set to default then the whole .zip needs to be downloaded anyways. - Write-Output "DOCKER $DockerPath" - if ($DockerPath -eq "default" -or $DockerDPath -eq "default") { - Write-Output "Checking Docker version" - $version = ((Invoke-WebRequest -Uri $DefaultDockerLocation).Links | Where-Object {$_.href -like "docker*"}).href | Sort-Object -Descending | Select-Object -First 1 | Select-String -Pattern "docker-(\d+\.\d+\.\d+).+" -AllMatches | Select-Object -Expand Matches | %{ $_.Groups[1].Value } + #If one of these are set to default then the whole .zip needs to be downloaded anyways. + Write-Output "DOCKER $DockerPath" + if ($DockerPath -eq "default" -or $DockerDPath -eq "default") { + Write-Output "Checking Docker versions" + #Get the list of .zip packages available from docker. + $availableVersions = ((Invoke-WebRequest -Uri $DefaultDockerLocation).Links | Where-Object {$_.href -like "docker*"}).href | Sort-Object -Descending + + #Parse the versions from the file names + $availableVersions = ($availableVersions | Select-String -Pattern "docker-(\d+\.\d+\.\d+).+" -AllMatches | Select-Object -Expand Matches | %{ $_.Groups[1].Value }) + $version = $availableVersions[0] - if($DockerVersion -ne "latest") { - $version = $DockerVersion - } else { - if(!(((Invoke-WebRequest -Uri $global:DefaultDockerLocation).Links | Where-Object {$_.href -like "*$global:DockerVersion*"}).href | Sort-Object -Descending | Select-Object -First 1)) { - Write-Error "Docker version supplied $global:DockerVersion was invalid, using the latest version." - } - } - - $zipUrl = $global:DefaultDockerLocation + "docker-$version.zip" - $destinationFolder = "$env:UserProfile\DockerDownloads" - - if(!(Test-Path "$destinationFolder")) { - md -Path $destinationFolder | Out-Null - } elseif(Test-Path "$destinationFolder\docker-$version") { - Remove-Item -Recurse -Force "$destinationFolder\docker-$version" - } - - Write-Output "Downloading $zipUrl to $destinationFolder\docker-$version.zip" - Copy-File -SourcePath $zipUrl -DestinationPath "$destinationFolder\docker-$version.zip" - Expand-Archive -Path "$destinationFolder\docker-$version.zip" -DestinationPath "$destinationFolder\docker-$version" - - if($DockerPath -eq "default") { - $DockerPath = "$destinationFolder\docker-$version\docker\docker.exe" - } - if($DockerDPath -eq "default") { - $DockerDPath = "$destinationFolder\docker-$version\docker\dockerd.exe" + if($DockerVersion -ne "latest") { + $version = $DockerVersion + if(!($availableVersions | Select-String $DockerVersion)) { + Write-Error "Docker version supplied $DockerVersion was invalid, please choose from the list of available versions: $availableVersions" + throw "Invalid docker version supplied." } } - } catch { - Write-Error "Failed to download the latest docker version: $_" + + $zipUrl = $global:DefaultDockerLocation + "docker-$version.zip" + $destinationFolder = "$env:UserProfile\DockerDownloads" + + if(!(Test-Path "$destinationFolder")) { + md -Path $destinationFolder | Out-Null + } elseif(Test-Path "$destinationFolder\docker-$version") { + Remove-Item -Recurse -Force "$destinationFolder\docker-$version" + } + + Write-Output "Downloading $zipUrl to $destinationFolder\docker-$version.zip" + Copy-File -SourcePath $zipUrl -DestinationPath "$destinationFolder\docker-$version.zip" + Expand-Archive -Path "$destinationFolder\docker-$version.zip" -DestinationPath "$destinationFolder\docker-$version" + + if($DockerPath -eq "default") { + $DockerPath = "$destinationFolder\docker-$version\docker\docker.exe" + } + if($DockerDPath -eq "default") { + $DockerDPath = "$destinationFolder\docker-$version\docker\dockerd.exe" + } } Write-Output "Installing Docker... $DockerPath"