Resolving container issues.

How to get the application logs to check if docker has failed?

  1.  Open powershell in administrator mode. 
  2. . Run the command "Get-EventLog -LogName Application -Source Docker -After (Get-Date).AddMinutes(-30)  | Sort-Object Time | Export-CSV ~/dockerlast30minutes.CSV"
  3.  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?

  1. Generally the docker.exe is in this location C:\Program Files\Docker\dockerd.exe
  2. If the BINARY_PATH_NAME = "C:\Program Files\Docker\dockerd.exe --run-service" then run the command with -D at the end.
 [sc.exe config docker binpath= "C:\Program Files\Docker\dockerd.exe --run-service -D"]
     
  1.  sc.exe stop docker
  2.  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. 

  1. sc.exe config docker binpath= "C:\Program Files\Docker\dockerd.exe --run-service"
  2. sc.exe stop docker
  3. sc.exe start docker
--------------------------------------------------------

 How to list the containers? 


  1. docker ps(lists all the running containers)
  2. docker ps -a (list all the containers including stopped containers)

--------------------------------------------------------
How to check the logs of stopped container? 

  1. When you run the command "docker ps -a", it will list the containers along with the status
  2. Copy the id of container and run the below command. 
docker logs  33c019953fb4 >> C:\Users\32514\Desktop\Log\application_api.txt
*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
  1. docker network ls
  2. docker network inspect hns
  3. sc.exe stop docker
  4. sc.exe stop hns
  5. Get-ContainerNetwork | Remove-ContainerNetwork
    1. Delete your HNS.data file "C:\ProgramData\Microsoft\Windows\HNS\HNS.data"
  6. sc.exe start hns
  7. sc.exe start docker

  • For Nat
  1. docker network ls
  2. docker network inspect nat
  3. sc.exe stop docker
  4. sc.exe stop nat
  5. Get-NetNat | Remove-NetNat
  6. sc.exe start nat
  7. 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. :)
  1. Uninstall-Module dockermsftprovider
  2. Uninstall-package docker
  3. Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
  4. Install-Package -Name docker -ProviderName DockerMsftProvider
  5. 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? 
  1. 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'

}


--------------------------------------------------------



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



Comments