Sitecore / docker-images

Build docker images for Sitecore
MIT License
179 stars 222 forks source link

Build continues when Docker is not running #206

Open michaellwest opened 4 years ago

michaellwest commented 4 years ago

Wrote a small snippet we could add to build.ps1 which performs a check.

image

$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
}

If you think this is a good feature to add I can submit a PR.

pbering commented 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.

michaellwest commented 4 years ago

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
    }
}
pbering commented 4 years ago

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"

michaellwest commented 4 years ago

First check if docker is running using the named pipes.

image

Running in Linux container mode.

image

Switching to Windows container mode.

image

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
}
pbering commented 4 years ago

Nice once, helpful messages. No sure if we should ask to switch or just switch? And the warnings should use Write-Warning.

michaellwest commented 4 years ago

This will switch the engine but the UI may not reflect the change. https://github.com/docker/for-win/issues/1269

michaellwest commented 4 years ago

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
}