重新启动docker Windows 10命令行

重新启动docker Windows 10命令行,docker,batch-file,windows-10,Docker,Batch File,Windows 10,我正在试图找出如何在命令行中重新启动docker,以便生成一个bat脚本来重新启动它并启动几个容器 我使用AdminAccess创建了一个dos提示符,并运行以下命令 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 occurre

我正在试图找出如何在命令行中重新启动docker,以便生成一个bat脚本来重新启动它并启动几个容器

我使用AdminAccess创建了一个dos提示符,并运行以下命令

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>
注意:我可以使用docker windows应用程序重新启动它。但是我需要做这个命令行


任何想知道我为什么重新启动docker的人的背景。每次重新启动时都这么做,我真是烦死了,所以我希望创建一个bat文件,我就可以开始了。

这是最新版本的Docker for Windows Desktop(Docker Community Edition 2.0.0.3 2019-02-15)


我禁用了“”,我不再有此问题,但有YMMV。

如果您可以使用Powershell作为windows命令行,则可以获得更可控和正确的结果(基于@sabujp的回答)。这也使用Docker Desktop 2.1.0.1(2019-08-19的当前版本)


我知道有一个docker服务,您可以使用SC从命令行重新启动服务:。你试过了吗?对于那些想在我的电脑上启动windows自动启动docker的人来说,你的命令运行得很好,但我不能继续使用我的指挥官,我有一个闪烁的卡雷特,我卡住了。这就是为什么我要在命令行之前添加start“”。我已经尝试使用cmd,你是对的,它工作正常。我没有得到与启动部分
s>net start docker匹配的服务名称无效。键入NET HELPMSG 2185可获得更多帮助。>net start com.docker.service系统出现错误5。访问被拒绝。>“c:\program files\docker\docker\docker for Windows.exe”c:\program files\docker\docker\docker for Windows.exe
我已经用脚本创建了一个bat文件,但当我执行它时,cmd将保留闪烁的插入符号,而不是退出@大卫,你能详细说明一下你为解决这个问题做了什么吗?我试图用
开始“c:\program files\docker\docker\docker Desktop.exe”
替换最后一行,但没有成功。顺便说一句,到2021年,Docker进程名为“Docker Desktop”,不再是“Docker for Windows”,我不得不用
Docker Desktop.exe
尼斯脚本替换
Docker for Windows.exe
,我想我们可以从Docker info中使用$LastExitCode,而不是解析字符串来确定Docker是否未运行,例如:
psc:\>$LASTEXITCODE;0 PS C:\>$info=(docker info)错误打印信息PS C:\>$LASTEXITCODE;1 PS C:\>
$LASTEXITCODE
在powershell和docker desktop中一直都很奇怪。如果它对你有用,那就去做吧。
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"
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"