Last active
September 11, 2020 17:10
-
-
Save rleap-m/d64a0d8c93eabf919a08c5ce580cdd8e to your computer and use it in GitHub Desktop.
Example of a container with a health check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Command line to run a PowerShell encoded command (allows you to avoid escaping special characters) | |
$runPoshEncodedCmd = 'powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand ' | |
# Command to run in the container - loop which outputs the date to a file and STDOUT | |
$command = "& { Write-Host `"The working directory is [`$(`$pwd.Path)]`";" | |
$command += " `$null = New-Item -Path c:\temp\ -ItemType Directory -ErrorAction SilentlyContinue;" | |
$command += " do { Get-Date -Format s | Tee-Object -FilePath C:\Temp\date_output2.txt -Append;" | |
$command += " Write-Host `"Line count: `$((Get-Content -Path C:\Temp\date_output2.txt).Count)`";" | |
$command += " Start-Sleep -Seconds 5} while (`$true) }" | |
# Health Check Command - once the number of lines in the date file gets over a certain amount container is unhealthy | |
$healthCommand = 'if ((Get-Content -Path C:\Temp\date_output2.txt).Count -gt 5) { exit 1 } else { exit 0 }' | |
[string] $encodedHealthCommand = [convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($healthCommand)) | |
# Docker Run | |
docker container run --name checkmyhealth --health-cmd ($runPoshEncodedCmd + $encodedHealthCommand) ` | |
--health-interval 10s --health-retries 2 --health-start-period 5s --health-timeout 5s ` | |
--rm -it mcr.microsoft.com/windows/servercore:ltsc2019 powershell -command $command | |
<# | |
# From another terminal on the host monitor the container (declining) health with this command | |
do { | |
(docker container inspect checkmyhealth | ConvertFrom-Json).State.Health | | |
Select-Object Status,FailingStreak; start-sleep 5 | |
} while ($true) | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment