Skip to content

Instantly share code, notes, and snippets.

@arcanisgk
Last active February 27, 2024 20:51
Show Gist options
  • Save arcanisgk/d78acd5d51ab263d9467fb2da97781ca to your computer and use it in GitHub Desktop.
Save arcanisgk/d78acd5d51ab263d9467fb2da97781ca to your computer and use it in GitHub Desktop.
docker-script.ps1
$ErrorActionPreference = 'Continue'
Stop-Service -Name "com.docker.*" -ErrorAction SilentlyContinue
Set-Service -Name "com.docker.service" -StartupType Automatic -ErrorAction SilentlyContinue
$processesToStop = @('Docker Desktop', 'com.docker.backend', 'com.docker.extensions')
$processesToStop | ForEach-Object {
Get-Process -Name $_ -ErrorAction Ignore | Stop-Process -Force -ErrorAction Ignore
}
$settingsToUpdate = @{
exposeDockerAPIOnTCP2375 = $true
updateHostsFile = $true
licenseTermsVersion = 2
noWindowsContainers = $false # Required to enable read updateHostsFile property
runWinServiceInWslMode = $true # Required to enable read updateHostsFile property
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) {
Write-Host $update.Key
Add-Member -InputObject $settingsObject -MemberType NoteProperty -Name $update.Key -Value $update.Value -Force
$trackUpdates++
}
}
}
if ($trackUpdates -eq 0) {
$ErrorActionPreference = 'Continue'
Stop-Service -Name "com.docker.*" -ErrorAction SilentlyContinue
Set-Service -Name "com.docker.service" -StartupType Automatic -ErrorAction SilentlyContinue
$processesToStop = @('Docker Desktop', 'com.docker.backend', 'com.docker.extensions')
$processesToStop | ForEach-Object {
Get-Process -Name $_ -ErrorAction Ignore | Stop-Process -Force -ErrorAction Ignore
}
$settingsToUpdate = @{
exposeDockerAPIOnTCP2375 = $true
updateHostsFile = $true
licenseTermsVersion = 2
noWindowsContainers = $false
runWinServiceInWslMode = $true
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 -ne 0) {
$settingsObject | ConvertTo-Json | Set-Content $settingsPath
}
Start-Service -Name "com.docker.service" -ErrorAction SilentlyContinue
while ((Get-Service -Name "com.docker.service").Status -ne "Running") {
Start-Sleep -Seconds 1
}
if((Get-Service -Name "com.docker.service").Status -eq "Running"){
$status = (Get-Service -Name "com.docker.service").Status
Write-Host "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 'Validando pipe line'
do {
Start-Sleep -Milliseconds 500
$pipeOpen = Test-Path -LiteralPath \\.\pipe\docker_engine
} until ($pipeOpen -or ($waitUntil -le [datetime]::Now))
if (-not $pipeOpen) {
Write-Warning "Error: pipe-line"
return
} else {
Write-Host "Working: pipe-line"
}
$responseTimeout = New-TimeSpan -Seconds 20
$waitUntil = [datetime]::Now.Add($responseTimeout)
$dockerInfoSuccess = $false
Write-Host 'Validando docker access'
do {
Start-Sleep -Milliseconds 500
$dockerInfoOutput = docker info 2>&1
$dockerInfoSuccess = $dockerInfoOutput -notmatch "ERROR:"
Write-Host $dockerInfoSuccess
} until ($dockerInfoSuccess -or ($waitUntil -le [datetime]::Now))
if (-not $dockerInfoSuccess) {
Write-Host $dockerInfoSuccess
Write-Warning "Error: docker access"
return
} else {
Write-Host "Working: docker access"
}
Write-Host 'Docker Desktop Is Runing Now'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment