How to get the application logs to check if docker has failed?
How to list the containers?
--------------------------------------------------------
- Open powershell in administrator mode.
- . Run the command "Get-EventLog -LogName Application -Source Docker -After (Get-Date).AddMinutes(-30) | Sort-Object Time | Export-CSV ~/dockerlast30minutes.CSV"
- Goto c:\users\userid and you will find the file "last30minutes.CSV
--------------------------------------------------------
How do you run the service with -D so that more information is logged?
- Generally the docker.exe is in this location C:\Program Files\Docker\dockerd.exe
- If the BINARY_PATH_NAME = "C:\Program Files\Docker\dockerd.exe --run-service" then run the command with -D at the end.
- sc.exe stop docker
- sc.exe start docker
-D switch puts a lots of more information in the application logs so here is the script to revert if you are done with event logs.
- sc.exe config docker binpath= "C:\Program Files\Docker\dockerd.exe --run-service"
- sc.exe stop docker
- sc.exe start docker
How to list the containers?
- docker ps(lists all the running containers)
- docker ps -a (list all the containers including stopped containers)
--------------------------------------------------------
How to check the logs of stopped container?
- When you run the command "docker ps -a", it will list the containers along with the status
- Copy the id of container and run the below command.
*Note you can use the Names or ID. Replace with your container ID
--------------------------------------------------------
How to check the logs in the container?
docker exec -it 33c019953fb4 cmd
docker cp 33c019953fb4:C:\Windows\System32\winevt\Logs
*Note you can use the Names or ID. Replace with your container ID
-------------------------------------------------------
Is there any other way to copy the logs?
// Create a folder on desktop called logs
docker cp application_api:/Windows/System32/winevt/Logs C:\Users\Srini\Desktop\Logs
*Note you can use the Names or ID. Replace with your container name
-------------------------------------------------------
How to inspect NAT and HNS?
- For Hns
- docker network ls
- docker network inspect hns
- sc.exe stop docker
- sc.exe stop hns
- Get-ContainerNetwork | Remove-ContainerNetwork
- Delete your HNS.data file "C:\ProgramData\Microsoft\Windows\HNS\HNS.data"
- sc.exe start hns
- sc.exe start docker
- For Nat
- docker network ls
- docker network inspect nat
- sc.exe stop docker
- sc.exe stop nat
- Get-NetNat | Remove-NetNat
- sc.exe start nat
- sc.exe start docker
If they both don't work
There is script provided by microsoft run the script
https://github.com/MicrosoftDocs/Virtualization-Documentation/blob/live/windows-server-container-tools/CleanupContainerHostNetworking/WindowsContainerNetworking-LoggingAndCleanupAide.ps1
Run the command ".\WindowsContainerNetworking-LoggingAndCleanupAide.ps1 -Cleanup"
There are other commands but use what suits you.
https://github.com/MicrosoftDocs/Virtualization-Documentation/tree/live/windows-server-container-tools/CleanupContainerHostNetworking
--------------------------------------------------------
That didn't work for me what's next?
- Uninstall and reinstall docker. :)
- Uninstall-Module dockermsftprovider
- Uninstall-package docker
- Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
- Install-Package -Name docker -ProviderName DockerMsftProvider
- Restart-Computer -Force
When you want to install specific version of docker use these commands.
Install-Package -Name docker -ProviderName DockerProvider -Force -MaximumVersion 17.03
--------------------------------------------------------
How to remove all docker containers that are not active?
- docker system prune -af (use with caution).
--------------------------------------------------------
How to update docker to latest version ?
- Install-Package -Name docker -ProviderName DockerMsftProvider -Update -Force
--------------------------------------------------------
Is there Powershell script to stop and start docker service?
$runningContainerNames = docker ps --format "{{.Names}}"
if ($runningContainerNames.Count -lt 1)
{
Write-Host -ForegroundColor Red 'There are no containers running.'
}
else
{
Write-Host -ForegroundColor Green 'There are containers running.'
}
$allContainerNames = docker ps -a --format "{{.Names}}"
if ($allContainerNames.Count -gt $runningContainerNames.Count)
{
Write-Host -ForegroundColor Red 'There are stopped containers and we need to restart the docker service.'
Write-Host -ForegroundColor Red 'The stopped containers are'
$stoppedContailers = $allContainerNames | ?{$runningContainerNames -notcontains $_}
Write-Host -ForegroundColor Red $stoppedContailers
cd\
cd windows\system32
Write-Host -ForegroundColor Cyan 'Stopping docker service.'
Stop-Service docker
$serviceNotStopped = $true
while ($serviceNotStopped -eq $true)
{
Start-Sleep -seconds 10
$service = get-service docker
if ($service.Status -eq 'Stopped')
{
$serviceNotStopped = $false
}
}
Write-Host -ForegroundColor Blue 'Successfully stopped docker service.'
Write-Host -ForegroundColor Cyan 'Starting docker service.'
Start-Service docker
$serviceNotStarted = $true
while ($serviceNotStarted -eq $true)
{
Start-Sleep -seconds 15
$service = get-service docker
if ($service.Status -eq 'Running')
{
$serviceNotStarted = $false
}
}
Write-Host -ForegroundColor Cyan 'Making sure all the containers are up and running.'
$runningContainerNames = docker ps --format "{{.Names}}"
$allContainerNames = docker ps -a --format "{{.Names}}"
if ($runningContainerNames.Count -eq $allContainerNames.Count)
{
Write-Host -ForegroundColor Green 'All containers are up and running.'
}
else
{
Write-Host -ForegroundColor Red 'Some containers are still not starting. The stopped containers are'
$stoppedContailers = $allContainerNames | ?{$runningContainerNames -notcontains $_}
Write-Host -ForegroundColor Red $stoppedContailers
Write-Host -ForegroundColor Red 'Please follow other troubleshooting steps.'
}
}
else
{
Write-Host -ForegroundColor Green 'There is no need to start the docker service to bring back the stopped containers'
}
if ($runningContainerNames.Count -lt 1)
{
Write-Host -ForegroundColor Red 'There are no containers running.'
}
else
{
Write-Host -ForegroundColor Green 'There are containers running.'
}
$allContainerNames = docker ps -a --format "{{.Names}}"
if ($allContainerNames.Count -gt $runningContainerNames.Count)
{
Write-Host -ForegroundColor Red 'There are stopped containers and we need to restart the docker service.'
Write-Host -ForegroundColor Red 'The stopped containers are'
$stoppedContailers = $allContainerNames | ?{$runningContainerNames -notcontains $_}
Write-Host -ForegroundColor Red $stoppedContailers
cd\
cd windows\system32
Write-Host -ForegroundColor Cyan 'Stopping docker service.'
Stop-Service docker
$serviceNotStopped = $true
while ($serviceNotStopped -eq $true)
{
Start-Sleep -seconds 10
$service = get-service docker
if ($service.Status -eq 'Stopped')
{
$serviceNotStopped = $false
}
}
Write-Host -ForegroundColor Blue 'Successfully stopped docker service.'
Write-Host -ForegroundColor Cyan 'Starting docker service.'
Start-Service docker
$serviceNotStarted = $true
while ($serviceNotStarted -eq $true)
{
Start-Sleep -seconds 15
$service = get-service docker
if ($service.Status -eq 'Running')
{
$serviceNotStarted = $false
}
}
Write-Host -ForegroundColor Cyan 'Making sure all the containers are up and running.'
$runningContainerNames = docker ps --format "{{.Names}}"
$allContainerNames = docker ps -a --format "{{.Names}}"
if ($runningContainerNames.Count -eq $allContainerNames.Count)
{
Write-Host -ForegroundColor Green 'All containers are up and running.'
}
else
{
Write-Host -ForegroundColor Red 'Some containers are still not starting. The stopped containers are'
$stoppedContailers = $allContainerNames | ?{$runningContainerNames -notcontains $_}
Write-Host -ForegroundColor Red $stoppedContailers
Write-Host -ForegroundColor Red 'Please follow other troubleshooting steps.'
}
}
else
{
Write-Host -ForegroundColor Green 'There is no need to start the docker service to bring back the stopped containers'
}
--------------------------------------------------------
How to set the above powershell script to run after every restart? After restart the containers fail to come up.
schtasks /create /tn "start" /sc onstart /delay 0000:30 /rl highest /ru system /tr "powershell.exe -file C:\DoNotDelete\DockerScripts\RestartDockerService.ps1"
----------------------------------------------------------
Updated script to bring back containers?
$runningContainerNames = docker ps --format "{{.Names}}"
if ($runningContainerNames.Count -lt 1)
{
Write-Host -ForegroundColor Red 'There are no containers running.'
}
else
{
Write-Host -ForegroundColor Green 'There are containers running.'
}
$allContainerNames = docker ps -a --format "{{.Names}}"
if ($allContainerNames.Count -gt $runningContainerNames.Count)
{
$exitedContainers = docker ps -f "status=exited" --format "{{.Names}}"
if ($exitedContainers.Count -gt 0)
{
Write-Host -ForegroundColor Red 'There are some exited containers. The exited containers are' $exitedContainers
Write-Host -ForegroundColor Green 'Trying to bring back the stopped containers'
foreach($exitedContainer in $exitedContainers)
{
Write-Host -ForegroundColor Green 'Trying to bring back the container' $exitedContainer
docker start $exitedContainer
Start-Sleep -s 10
}
}
$exitedContainers = docker ps -f "status=exited" --format "{{.Names}}"
if($exitedContainers.Count -gt 0)
{
$confirmation = Read-Host 'Do you want me to restart service? There are containers . Press "Y" or "N"'
if ($confirmation -eq 'y' -Or $confirmation -eq 'Y')
{
cd\
cd windows\system32
Write-Host -ForegroundColor Cyan 'Stopping docker service.'
Stop-Service docker
$serviceNotStopped = $true
while ($serviceNotStopped -eq $true)
{
Start-Sleep -seconds 10
$service = get-service docker
if ($service.Status -eq 'Stopped')
{
$serviceNotStopped = $false
}
}
Write-Host -ForegroundColor Blue 'Successfully stopped docker service.'
Write-Host -ForegroundColor Cyan 'Starting docker service.'
Start-Service docker
$serviceNotStarted = $true
while ($serviceNotStarted -eq $true)
{
Start-Sleep -seconds 15
$service = get-service docker
if ($service.Status -eq 'Running')
{
$serviceNotStarted = $false
}
}
Start-Sleep -seconds 10
Write-Host -ForegroundColor Cyan 'Making sure all the containers are up and running.'
$runningContainerNames = docker ps --format "{{.Names}}"
$allContainerNames = docker ps -a --format "{{.Names}}"
if ($runningContainerNames.Count -eq $allContainerNames.Count)
{
Write-Host -ForegroundColor Green 'All containers are up and running.'
}
else
{
Write-Host -ForegroundColor Red 'Some containers are still not starting. The stopped containers are'
$stoppedContailers = $allContainerNames | ?{$runningContainerNames -notcontains $_}
Write-Host -ForegroundColor Red $stoppedContailers
Write-Host -ForegroundColor Red 'Please follow other troubleshooting steps.'
}
}
}
else
{
Write-Host -ForegroundColor Green 'There is no need to start the docker service to bring back the stopped containers'
}
}
else
{
Write-Host -ForegroundColor Green 'There is no need to start the containers'
}
----------------------------------------------------------
Updated script to bring back containers?
$runningContainerNames = docker ps --format "{{.Names}}"
if ($runningContainerNames.Count -lt 1)
{
Write-Host -ForegroundColor Red 'There are no containers running.'
}
else
{
Write-Host -ForegroundColor Green 'There are containers running.'
}
$allContainerNames = docker ps -a --format "{{.Names}}"
if ($allContainerNames.Count -gt $runningContainerNames.Count)
{
$exitedContainers = docker ps -f "status=exited" --format "{{.Names}}"
if ($exitedContainers.Count -gt 0)
{
Write-Host -ForegroundColor Red 'There are some exited containers. The exited containers are' $exitedContainers
Write-Host -ForegroundColor Green 'Trying to bring back the stopped containers'
foreach($exitedContainer in $exitedContainers)
{
Write-Host -ForegroundColor Green 'Trying to bring back the container' $exitedContainer
docker start $exitedContainer
Start-Sleep -s 10
}
}
$exitedContainers = docker ps -f "status=exited" --format "{{.Names}}"
if($exitedContainers.Count -gt 0)
{
$confirmation = Read-Host 'Do you want me to restart service? There are containers . Press "Y" or "N"'
if ($confirmation -eq 'y' -Or $confirmation -eq 'Y')
{
cd\
cd windows\system32
Write-Host -ForegroundColor Cyan 'Stopping docker service.'
Stop-Service docker
$serviceNotStopped = $true
while ($serviceNotStopped -eq $true)
{
Start-Sleep -seconds 10
$service = get-service docker
if ($service.Status -eq 'Stopped')
{
$serviceNotStopped = $false
}
}
Write-Host -ForegroundColor Blue 'Successfully stopped docker service.'
Write-Host -ForegroundColor Cyan 'Starting docker service.'
Start-Service docker
$serviceNotStarted = $true
while ($serviceNotStarted -eq $true)
{
Start-Sleep -seconds 15
$service = get-service docker
if ($service.Status -eq 'Running')
{
$serviceNotStarted = $false
}
}
Start-Sleep -seconds 10
Write-Host -ForegroundColor Cyan 'Making sure all the containers are up and running.'
$runningContainerNames = docker ps --format "{{.Names}}"
$allContainerNames = docker ps -a --format "{{.Names}}"
if ($runningContainerNames.Count -eq $allContainerNames.Count)
{
Write-Host -ForegroundColor Green 'All containers are up and running.'
}
else
{
Write-Host -ForegroundColor Red 'Some containers are still not starting. The stopped containers are'
$stoppedContailers = $allContainerNames | ?{$runningContainerNames -notcontains $_}
Write-Host -ForegroundColor Red $stoppedContailers
Write-Host -ForegroundColor Red 'Please follow other troubleshooting steps.'
}
}
}
else
{
Write-Host -ForegroundColor Green 'There is no need to start the docker service to bring back the stopped containers'
}
}
else
{
Write-Host -ForegroundColor Green 'There is no need to start the containers'
}
Comments
Post a Comment