0

Docker-Desktop UI/GUI becomes unusable after running this script from powershell.

$ErrorActionPreference = 'Continue'
Set-Service -Name "com.docker.service" -StartupType Automatic -ErrorAction SilentlyContinue
Stop-Service -Name "com.docker.service" -ErrorAction SilentlyContinue
Get-Process 'Docker Desktop' -ErrorAction Ignore | Stop-Process -Force -ErrorAction Stop
Wait-Process -Name 'Docker Desktop' -ErrorAction SilentlyContinue
$settingsToUpdate = @{
    exposeDockerAPIOnTCP2375 = $true
    updateHostsFile = $true
    licenseTermsVersion = 2
    noWindowsContainers = $true
    runWinServiceInWslMode = $false
    useResourceSaver = $false
}

$settingsPath = "$env:APPDATA\Docker\settings.json"
$settingsContent = Get-Content $settingsPath -Raw 
$settingsObject  = $settingsContent | ConvertFrom-Json

$trackUpdates = 0
foreach ($update in $settingsToUpdate.GetEnumerator()) {
    if ($target = $settingsObject.psobject.Properties.Match($update.Key)) {
        if ($target.Value -ne $update.Value) {
            Add-Member -InputObject $settingsObject -MemberType NoteProperty -Name $update.Key -Value $update.Value -Force
            $trackUpdates++
        }
    }
}

if ($trackUpdates -eq 0) {
    Write-Host "No new settings applied"
} else {
    $settingsObject | ConvertTo-Json | Set-Content $settingsPath
    Write-Host "Settings updated and saved successfully"
}

Start-Service -Name "com.docker.service" -ErrorAction SilentlyContinue

while ((Get-Service -Name "com.docker.service").Status -ne "Running") {
    Write-Host (Get-Service -Name "com.docker.service").Status
    Start-Sleep -Seconds 1
}

if((Get-Service -Name "com.docker.service").Status -eq "Running"){
    Write-Host (Get-Service -Name "com.docker.service").Status
}

$dockerDesktopFilePath = $env:ProgramFiles | Join-Path -ChildPath 'Docker\Docker\Docker Desktop.exe'; Start-Process -FilePath $dockerDesktopFilePath

$ipcTimeout = New-TimeSpan -Seconds 20
$waitUntil = [datetime]::Now.Add($ipcTimeout)
$pipeOpen = $false
Write-Host 'Probing docker engine I/O pipe'
do {
  Start-Sleep -Milliseconds 100
  $pipeOpen = Test-Path -LiteralPath \\.\pipe\docker_engine
} until ($pipeOpen -or ($waitUntil -le [datetime]::Now))

if (-not $pipeOpen) {
  Write-Warning "Failed to observe named IPC pipe docker_engine within timeout"
  return
}

$responseTimeout = New-TimeSpan -Seconds 5
$waitUntil = [datetime]::Now.Add($responseTimeout)

Write-Host 'Querying docker server info'
do {
  Start-Sleep -Milliseconds 500
  $dockerInfoOutput = docker info 2>&1 
  $dockerInfoSuccess = $?
} until ($dockerInfoSuccess -or ($waitUntil -le [datetime]::Now))

if (-not $dockerInfoSuccess) {
  Write-Warning "docker info failed within timeout"
  return
}

Write-Host 'Docker Desktop Is Runing Now'

The script works correctly, the problem is the behavior of the Docker-desktop after its execution.

I have carried out several tests, since I have other related posts on other sites on the network, but I cannot find a solution.

Update:

So the problem is not how the application is opened, but how it is closed…

a friend try to run this script:

$ServiceName = "com.docker.service"
$ProcessName = "Docker Desktop"

$arrService = Get-Service -Name $ServiceName
if ($arrService.Status -ne 'Running') {
    Stop-Service -Name $ServiceName
}


if (Get-Process $ProcessName -ErrorAction SilentlyContinue) {  
  Stop-Process -Name $ProcessName
  Wait-Process -Name $ProcessName
}

In my case, the service would not be running, so the upper section of the code does not matter. In the task manager I could see that 4 or respectively 6 processes are running in relation to Docker desktop. So that the Stop-Process command terminated the porcess “Docker Desktop” immediately. But 2 processes “Docker Desktop Backend” and “Docker Desktop Extensions” are still running. I started Docker Desktop by double-clicking on a shortcut and could then see that the Docker Engine was paused and the application was not running stably. I then terminated all processes and was able to open Docker Desktop again without any problems. From here on, you have to find out everything else for yourself.

it is not clear to me how many processes have to be closed when closing Docker-Desktop, it seems that the closing requires a wild-card: "Docker-Desktop*" or "Docker Desktop*" or "*Docker*" but this can cause processes that should be kept running to close.

On the other hand I found something curious last night when I did installation #64 from 0.

It turns out that if you have docker running and you go to the settings for the first time and check the option to update the hosts file, the updateHostsFile property is not the only one that changes; These other 2 also change: noWindowsContainers, runWinServiceInWslMode and screw up the general docker configuration, if you are using Windows 11/10 home, since these options should only be enabled if you are using the PRO version with Hiper-V.

So for this reason, if you programmatically activate updateHostsFile it will have no effect… unless you activate the other two… which will not have positive results either…

The thing is complicated and complex… it should be simpler to manage this through PowerShell…

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.