How to restart docker for windows process in powershell?

21,317

Solution 1

Kill and restart the docker process:

$processes = Get-Process "*docker desktop*"
if ($processes.Count -gt 0)
{
    $processes[0].Kill()
    $processes[0].WaitForExit()
}
Start-Process "C:\Program Files\Docker\Docker\Docker Desktop.exe"

In the if clause I check if any running docker process has been found. There should never be more than 1 instance of "Docker Desktop" running so you can then kill the first one in the list.

To restart you need to know the full path of the "Docker Desktop.exe" file on your computer.

Solution 2

You can user in powershell:

restart-service *docker*

Or int the Docker QuickStart Terminal:

docker-machine restart

Solution 3

Similar to Sebastian L's comment above, but slightly cleaner & faster if you know whether you are currently running Linux or Windows containers.

If running Linux Containers

    Stop-Service *docker*        
    Start-Service *docker*
    &$Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchLinuxEngine

If running Windows Containers

    Stop-Service *docker*        
    Start-Service *docker*
    &$Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchWindowsEngine

-SwitchDaemon toggles from one to the other (Linux to Windows or Windows to Linux) which is why you have to do it twice.

Share:
21,317
pwxcoo
Author by

pwxcoo

print("hello, pwxcoo") printf("hello, pwxcoo") System.out.println("hello, pwxcoo") Console.WriteLine("hello, pwxcoo") console.log("hello, pwxcoo")

Updated on July 09, 2022

Comments

  • pwxcoo
    pwxcoo almost 2 years

    I want to restart docker for windows (now known as Docker Desktop) in powershell.

    I would like to do it with one command in PowerShell.

    enter image description here

    May I implement it?

    When using Restart-Service *docker*:

    enter image description here