PowerShell: Detect and redirect user to install Docker Desktop for Windows

As part of installing the Sitecore MVP Site on a brand new Windows 11 machine, I executed the Start-Environment.ps1 without installing any pre-requisites. 

The following error was thrown since I hadn't installed Docker for Windows, which is one of the pre-requisites.

I wanted to enclose the area of code that invokes Docker commands within a safety net and present better messages to the user rather than assuming Docker is installed in the machine. 

The error is due to this function call in Start-Environment.ps1:


The above line invokes the below function:



In a machine where Docker for Windows is installed, the following folder will be present:



So, the code should add a condition that checks for such a folder and exit execution with a message to install Docker Desktop for Windows as follows:



The same applied for Stop-Environment.ps1:






Condition Code:


if ([Environment]::Is64BitProcess -eq [Environment]::Is64BitOperatingSystem)
{
$programFilesPath=${env:ProgramFiles}
}else
{
$programFilesPath=${env:ProgramFiles(x86)}
}

$dockerFolder= "$programFilesPath\docker\docker";

#just for safety, checking for one of the exe in the folder

$dockerApp="$programFilesPath\docker\docker\Docker Desktop.exe";

if (!(Test-Path "$dockerFolder")-or !(Test-Path "$dockerApp")) {

Write-Host "Restart execution after installing Docker Desktop for Windows from https://docs.docker.com/desktop/windows/install/" -ForegroundColor Yellow

exit 0

}


Error:



& : The term 'docker-compose' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At C:\mvp-site\docker\tools\Scripts\Functions-Docker.ps1:17 char:10

+        & "docker-compose" $fileArgs "build" "-m" "$MemoryLimit"

+          ~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (docker-compose:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


& : The term 'docker-compose' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At C:\mvp-site\docker\tools\Scripts\Functions-Docker.ps1:19 char:7

+     & "docker-compose" $fileArgs "up" "-d"

+       ~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (docker-compose:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


Waiting for CM to become available...

The property 'StatusCode' cannot be found on this object. Verify that the property exists.

At C:\mvp-site\docker\tools\Scripts\Functions-SitecoreCLI.ps1:23 char:17

+             if ($_.Exception.Response.StatusCode.value__ -ne "404") {

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException

    + FullyQualifiedErrorId : PropertyNotFoundStrict


The variable '$status' cannot be retrieved because it has not been set.

At C:\mvp-site\docker\tools\Scripts\Functions-SitecoreCLI.ps1:27 char:14

+     } while ($status.status -ne "enabled" -and $startTime.AddSeconds( ...

+              ~~~~~~~

    + CategoryInfo          : InvalidOperation: (status:String) [], RuntimeException

    + FullyQualifiedErrorId : VariableIsUndefined


The variable '$status' cannot be retrieved because it has not been set.

At C:\mvp-site\docker\tools\Scripts\Functions-SitecoreCLI.ps1:28 char:14

+     if (-not $status.status -eq "enabled") {

+              ~~~~~~~

    + CategoryInfo          : InvalidOperation: (status:String) [], RuntimeException

    + FullyQualifiedErrorId : VariableIsUndefined

Comments