Open michaellwest opened 4 years ago
It's a good feature but checking \\.\\pipe\\
doesn't work on Linux since it's a socket instead.
I guess you could run docker info
in a try/catch statement and then check $LASTEXITCODE instead.
Assuming that build.ps1 is only ever run on a Windows machine then it's likely a good partial solution. The primary goal is to help the larger audience building on windows and unfamiliar with Docker.
The following adds an additional check.
if ($env:OS -eq "WindowsNT" -or $IsWindows)
{
$pipeResult = [System.IO.Directory]::GetFiles("\\.\\pipe\\") | Select-String -Pattern "docker_engine_windows"
if ([string]::IsNullOrEmpty($pipeResult))
{
Write-Error "Docker for Windows may not be running or set to Windows containers mode."
exit
}
}
Ah we are only talking about Build.ps1, missed that... Instead of failing you could switch the engine to Windows with & (Join-Path $env:PROGRAMFILES "\Docker\Docker\DockerCli.exe") -SwitchWindowsEngine
and only fail in case of $env:OS -ne "Windows_NT"
First check if docker is running using the named pipes.
Running in Linux container mode.
Switching to Windows container mode.
if ($env:OS -eq "Windows_NT" -or $IsWindows)
{
$pipeResult = [System.IO.Directory]::GetFiles("\\.\\pipe\\") | Select-String -Pattern "docker_engine"
if ([string]::IsNullOrEmpty($pipeResult))
{
Write-Host "Docker for Windows may not be running. Please start up and try again."
exit
}
$ostype = docker info | Select-String OSType
if ($ostype -like "*linux*")
{
Write-Host "WARNING!: Docker is currently running in Linux container mode."
$response = Read-Host "Would you like to switch to Windows container mode? [y/N]"
if ($response -match "y(es)?")
{
& (Join-Path $env:PROGRAMFILES "\Docker\Docker\DockerCli.exe") -SwitchWindowsEngine
Write-Host "Switched to the Windows container mode"
}
else
{
Write-Host "Stopping the build process."
exit
}
}
}
else
{
Write-Host "The script appears to be running on a non-Windows machine."
exit
}
Nice once, helpful messages. No sure if we should ask to switch or just switch? And the warnings should use Write-Warning.
This will switch the engine but the UI may not reflect the change. https://github.com/docker/for-win/issues/1269
Latest version. Just go ahead and switch.
if ($env:OS -eq "Windows_NT" -or $IsWindows)
{
$pipeResult = [System.IO.Directory]::GetFiles("\\.\\pipe\\") | Select-String -Pattern "docker_engine"
if ([string]::IsNullOrEmpty($pipeResult))
{
Write-Warning "Docker for Windows may not be running. Please start up and try again."
exit
}
$ostype = docker info | Select-String OSType
if ($ostype -like "*linux*")
{
Write-Host "Docker is currently running in Linux container mode."
& (Join-Path $env:PROGRAMFILES "\Docker\Docker\DockerCli.exe") -SwitchWindowsEngine
Write-Host "Switched to the Windows container mode"
}
}
else
{
Write-Warning "The script appears to be running on a non-Windows machine."
exit
}
Wrote a small snippet we could add to build.ps1 which performs a check.
If you think this is a good feature to add I can submit a PR.