24

I am trying to figure out how to restart docker in the command line so that i can make a bat script to restart it and start up a few containers.

I created a dos prompt using admin access and run the following

PS C:\Windows\system32> net stop com.docker.service
The Docker for Windows Service service is stopping.
A system error has occurred.

System error 1067 has occurred.

The process terminated unexpectedly.

The Docker for Windows Service service was stopped successfully.

PS C:\Windows\system32> net start com.docker.service
The Docker for Windows Service service is starting.
The Docker for Windows Service service was started successfully.

PS C:\Windows\system32> docker ps
error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.25/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error ma
y also indicate that the docker daemon is not running.
PS C:\Windows\system32>

Note: i can restart it fine using the docker windows app. However I need to do this comandline.

Background for anyone wondering why i am restarting docker Docker won't start containers after win 10 shutdown and power up.. I am sick to death of doing this every time i reboot so was hoping to create a bat file i could just kick off.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I know there's a docker service, and you can use SC to restart services from the command line: https://stackoverflow.com/questions/133883/stop-and-start-a-service-via-batch-or-cmd-file. Have you tried that? – slim Dec 18 '18 at 16:00
  • For those looking to autostart docker on windows start-up https://thecodeframework.com/start-docker-desktop-on-windows-start-up-without-user-logon/ – Gagan Aug 02 '20 at 02:58

4 Answers4

23

This is with more recent versions of Docker for Windows Desktop (Docker Community Edition 2.0.0.3 2019-02-15)

net stop docker
net stop com.docker.service
taskkill /IM "dockerd.exe" /F
taskkill /IM "Docker for Windows.exe" /F
net start docker
net start com.docker.service
"c:\program files\docker\docker\Docker for Windows.exe"
sabujp
  • 989
  • 1
  • 11
  • 12
  • On my computer your command works just fine but I cannot continue using my commander, I have a blinking carret and I'm stuck. That's why I would have add start "" before the command line. – David C Apr 08 '19 at 13:52
  • I've tried with cmd and you are right, it works fine. – David C Apr 08 '19 at 13:55
  • 1
    I'm getting no match for the startup part ```s > net start docker The service name is invalid. More help is available by typing NET HELPMSG 2185. > net start com.docker.service System error 5 has occurred. Access is denied. > "c:\program files\docker\docker\Docker for Windows.exe" c:\program files\docker\docker\Docker for Windows.exe ``` – Alsushi May 09 '20 at 16:26
  • 2
    I've made a bat file with the script but when I execute it the cmd remains with the blinking caret instead of exiting. @DavidC could you elaborate more on what you did to solve this issue? I've tried to replace the last line with `start "c:\program files\docker\docker\Docker Desktop.exe"` with no success. By the way, as of 2021 the Docker process is named "Docker Desktop" and no longer "Docker for Windows" – Maxiride Mar 22 '21 at 09:28
  • 3
    I had to replace `Docker for Windows.exe` with `Docker Desktop.exe` – Megidd Jun 05 '21 at 04:29
20

If you can use Powershell as the windows command line, you can get a more controlled and correct result (based on @sabujp's answer). This is also using Docker Desktop 2.1.0.1 (current as of 2019-08-19).

Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Restarting docker"

foreach($svc in (Get-Service | Where-Object {$_.name -ilike "*docker*" -and $_.Status -ieq "Running"}))
{
    $svc | Stop-Service -ErrorAction Continue -Confirm:$false -Force
    $svc.WaitForStatus('Stopped','00:00:20')
}

Get-Process | Where-Object {$_.Name -ilike "*docker*"} | Stop-Process -ErrorAction Continue -Confirm:$false -Force

foreach($svc in (Get-Service | Where-Object {$_.name -ilike "*docker*" -and $_.Status -ieq "Stopped"} ))
{
    $svc | Start-Service 
    $svc.WaitForStatus('Running','00:00:20')
}

Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Starting Docker Desktop"
& "C:\Program Files\Docker\Docker\Docker Desktop.exe"
$startTimeout = [DateTime]::Now.AddSeconds(90)
$timeoutHit = $true
while ((Get-Date) -le $startTimeout)
{

    Start-Sleep -Seconds 10
    $ErrorActionPreference = 'Continue'
    try
    {
        $info = (docker info)
        Write-Verbose "$((Get-Date).ToString("HH:mm:ss")) - `tDocker info executed. Is Error?: $($info -ilike "*error*"). Result was: $info"

        if ($info -ilike "*error*")
        {
            Write-Verbose "$((Get-Date).ToString("HH:mm:ss")) - `tDocker info had an error. throwing..."
            throw "Error running info command $info"
        }
        $timeoutHit = $false
        break
    }
    catch 
    {

        if (($_ -ilike "*error during connect*") -or ($_ -ilike "*errors pretty printing info*")  -or ($_ -ilike "*Error running info command*"))
        {
            Write-Output "$((Get-Date).ToString("HH:mm:ss")) -`t Docker Desktop startup not yet completed, waiting and checking again"
        }
        else
        {
            Write-Output "Unexpected Error: `n $_"
            return
        }
    }
    $ErrorActionPreference = 'Stop'
}
if ($timeoutHit -eq $true)
{
    throw "Timeout hit waiting for docker to startup"
}

Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Docker restarted"
StingyJack
  • 19,041
  • 10
  • 63
  • 122
  • Nice script, I think we can use $LastExitCode from docker info instead of parsing strings to determine if docker is not running, eg: `PS C:\> $LASTEXITCODE; 0 PS C:\> $info = (docker info) errors pretty printing info PS C:\> $LASTEXITCODE; 1 PS C:\> ` – Emil G Feb 20 '20 at 08:47
  • `$LASTEXITCODE` has always been weird in powershell and so has docker desktop. If it works for you, go for it. – StingyJack Feb 25 '20 at 19:54
  • 1
    This is the only solution that works for me as oh Docker Desktop on recent Windows 10 builds. Since the engine can only be restarted from the UI when the engine is running, the frequently engine crashes on Windows make this the only viable solution. Works as of Docker 20.10.6. – Alex Huszagh Jun 19 '21 at 17:23
0

"I´m solved this problem just making a logout and a login into docker desktop."

ref: https://forums.docker.com/t/restart-docker-service-from-command-line/27331/16

andreagalle
  • 620
  • 6
  • 17
-1

I disabled "fast startup" and I no longer have this issue but YMMV.

slim
  • 2,545
  • 1
  • 24
  • 38