Modules/businessdev.ALbuild.Containers/Public/Test-BcDocker.ps1
|
function Test-BcDocker { <# .SYNOPSIS Reports whether the Docker CLI is installed and its daemon is reachable. .DESCRIPTION Returns an object describing Docker availability. Unlike Test-BcPlatform (which only checks for the CLI on PATH), this actually queries the Docker daemon for its version, so it can distinguish "Docker installed but not running" from "Docker ready". .PARAMETER DockerExecutable The Docker executable to query. Default 'docker'. .PARAMETER Require Throw if Docker is not installed or the daemon is not reachable. .EXAMPLE if ((Test-BcDocker).DaemonRunning) { ... } .OUTPUTS PSCustomObject with Installed, DaemonRunning, ClientVersion, ServerVersion. #> [CmdletBinding()] [OutputType([PSCustomObject])] param( [string] $DockerExecutable = 'docker', [switch] $Require ) $installed = [bool](Get-Command -Name $DockerExecutable -CommandType Application -ErrorAction SilentlyContinue) $clientVersion = $null $serverVersion = $null $daemonRunning = $false if ($installed) { $client = Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -PassThru ` -Arguments @('version', '--format', '{{.Client.Version}}') if ($client.Success) { $clientVersion = $client.StdOut.Trim() } $server = Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -PassThru ` -Arguments @('version', '--format', '{{.Server.Version}}') if ($server.Success -and -not [string]::IsNullOrWhiteSpace($server.StdOut)) { $serverVersion = $server.StdOut.Trim() $daemonRunning = $true } } $result = [PSCustomObject]@{ Installed = $installed DaemonRunning = $daemonRunning ClientVersion = $clientVersion ServerVersion = $serverVersion } if ($Require -and -not $daemonRunning) { $reason = if (-not $installed) { 'the Docker CLI was not found on PATH' } else { 'the Docker daemon is not reachable (is Docker running?)' } throw "Docker is required but $reason. Business Central containers require a running Docker engine on a Windows host." } return $result } |