WSL2 の導入が不十分な状態で Docker Desktop の Use the WSL2 based engine を有効にしてしまったときの対応策

# 環境

- Windows 10 Pro 2004 (19041)
- Docker Desktop 2.3.0.4

# 背景

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/49525/c2364eac-6b19-953c-59c3-c6a4870972c6.png)

WSL2 の導入が不十分な状態 (※) で Docker Desktop の Use the WSL2 based engine を有効にしてしまうと、場合によっては Docker Desktop が起動できなくなってしまう。
※ 筆者の場合は VPN ソフトウェアが起動しているときに WSL2 をインストールしてしまったがために、インストールしたものの WSL2 の起動ができない状態に陥ってしまっていた。
https://github.com/microsoft/WSL/issues/5351

一度こうなってしまった場合、Docker Desktop の Factory Reset をかけても、再インストールしても改善しなかった。(Use the WSL2 based engine が ☑ の状態。)

# 解決策

```text
Docker Desktop を起動 → [起動中] → エラーで落ちる
```

の「起動中」のあいだにタスクトレイから Docker Desktop の設定ウインドウを開くこと。
その後、再度 Use the WSL2 based engine のチェックを外せばよい。

設定ウインドウが開いてしまえばもう大丈夫。
Apply ボタンを押した時点で Docker Desktop がエラーで落ちている場合、設定が完了しないように見えるが、かまわずウインドウを手動で閉じてよい。

再度 Docker Desktop を起動し直せば、WSL2 のチェックが外された状態で起動するはず。

# メモ: Docker Desktop に出力されたエラーメッセージ

```text
[16:02:30.382][LoggingMessageHandler][Info   ] [5b30b70a] <BackendAPIClient end> POST http://backend/hyperv/stop -> 204 NoContent (took 9149ms)
[16:02:30.387][ApiProxy          ][Info   ] Killing existing com.docker.proxy with PID 19304
[16:02:30.429][VpnKit            ][Info   ] Killing existing vpnkit with PID 16264
[16:02:30.458][VpnKitBridge      ][Info   ] Killing existing vpnkit-bridge with PID 2208
[16:02:30.482][EngineStateMachine][Debug  ] sending state Docker.ApiServices.StateMachines.StoppedState to state change sink
[16:02:30.482][EngineStateMachine][Debug  ] State Docker.ApiServices.StateMachines.StoppedState sent to state change sink
[16:02:30.482][EngineStateListener][Debug  ] received state Docker.ApiServices.StateMachines.StoppedState from LinuxHyperV
[16:02:30.483][EngineStateNotificationRecorder][Debug  ] Registered state {"State":"stopped","Mode":"linux","date":1599634950}
[16:02:30.483][SystrayNotifications][Info   ] Docker has stopped
[16:02:30.483][LoggingMessageHandler][Info   ] [a1a2394b] <NotificationsClient start> POST http://localhost/engine/state
[16:02:30.492][LoggingMessageHandler][Info   ] [a1a2394b] <NotificationsClient end> POST http://localhost/engine/state -> 200 OK (took 8ms)
[16:02:30.501][Engines           ][Debug  ] Starting
[16:02:30.502][LoggingMessageHandler][Info   ] [abeb5d24] <BackendAPIClient start> POST http://backend/versionpack/enable
[16:02:30.520][LoggingMessageHandler][Info   ] [abeb5d24] <BackendAPIClient end> POST http://backend/versionpack/enable -> 204 NoContent (took 18ms)
[16:02:30.520][EngineStateMachine][Debug  ] sending state Docker.ApiServices.StateMachines.StartTransition to state change sink
[16:02:30.520][EngineStateMachine][Debug  ] State Docker.ApiServices.StateMachines.StartTransition sent to state change sink
[16:02:30.521][EngineStateListener][Debug  ] received state Docker.ApiServices.StateMachines.StartTransition from LinuxWSL2
[16:02:30.521][EngineStateNotificationRecorder][Debug  ] Registered state {"State":"starting","Mode":"linux","date":1599634950}
[16:02:30.521][SystrayNotifications][Info   ] Docker is starting
[16:02:30.522][LoggingMessageHandler][Info   ] [fdbe74be] <NotificationsClient start> POST http://localhost/engine/state
[16:02:30.527][LinuxWSL2Engine   ][Info   ] Terminating lingering processes and wsl distros and patching host file
[16:02:30.527][LoggingMessageHandler][Info   ] [15e2371f] <BackendAPIClient start> POST http://backend/dns/refresh-hosts
[16:02:30.532][LoggingMessageHandler][Info   ] [fdbe74be] <NotificationsClient end> POST http://localhost/engine/state -> 200 OK (took 10ms)
[16:02:30.604][LoggingMessageHandler][Info   ] [15e2371f] <BackendAPIClient end> POST http://backend/dns/refresh-hosts -> 204 NoContent (took 77ms)
[16:02:30.609][WSL2Provisioning  ][Info   ] Checking docker-desktop
[16:02:30.613][WSL2Provisioning  ][Info   ] deploying WSL distro docker-desktop to C:\Users\ogawa\AppData\Local\Docker\wsl\distro
[16:02:31.338][WSL2Provisioning  ][Error  ] Failed to set version to docker-desktop: exit code: -1
 stdout: ?[L?W0?0F0h0W0_0?d\Oo00?SgqW0_0?0?0?0?0?0?0n0.z^?g0o0?0?0?0?0U0?0f0D0~0[0?00
 
 stderr: 
[16:02:31.343][LinuxWSL2Engine   ][Info   ] Stopping windows side processes
[16:02:31.418][LinuxWSL2Engine   ][Info   ] Stopping engine
[16:02:31.529][LinuxWSL2Engine   ][Warning] Failed to send stop message to distro, will terminate abrubtly: 
[16:02:31.647][LoggingMessageHandler][Info   ] [fe4e5f2d] <BackendAPIClient start> POST http://backend/windowsfeatures/check
[16:02:32.062][LoggingMessageHandler][Info   ] [fe4e5f2d] <BackendAPIClient end> POST http://backend/windowsfeatures/check -> 200 OK (took 414ms)
[16:02:32.607][LinuxWSL2Engine   ][Info   ] Terminating lingering processes and wsl distros and patching host file
[16:02:32.607][LoggingMessageHandler][Info   ] [84dcd8a7] <BackendAPIClient start> POST http://backend/dns/refresh-hosts
[16:02:32.667][WSL2Provisioning  ][Info   ] Checking docker-desktop
[16:02:32.667][WSL2Provisioning  ][Info   ] deploying WSL distro docker-desktop to C:\Users\ogawa\AppData\Local\Docker\wsl\distro
[16:02:32.686][LoggingMessageHandler][Info   ] [84dcd8a7] <BackendAPIClient end> POST http://backend/dns/refresh-hosts -> 204 NoContent (took 77ms)
[16:02:33.320][WSL2Provisioning  ][Error  ] Failed to set version to docker-desktop: exit code: -1
 stdout: ?[L?W0?0F0h0W0_0?d\Oo00?SgqW0_0?0?0?0?0?0?0n0.z^?g0o0?0?0?0?0U0?0f0D0~0[0?00
 
 stderr: 
[16:02:33.320][LinuxWSL2Engine   ][Info   ] Stopping windows side processes
[16:02:33.387][LinuxWSL2Engine   ][Info   ] Stopping engine
[16:02:33.473][LinuxWSL2Engine   ][Warning] Failed to send stop message to distro, will terminate abrubtly: 
[16:02:33.556][EngineStateMachine][Debug  ] sending state Docker.ApiServices.StateMachines.FailedToStartState to state change sink
[16:02:33.556][EngineStateMachine][Debug  ] State Docker.ApiServices.StateMachines.FailedToStartState sent to state change sink
[16:02:33.556][EngineStateListener][Debug  ] received state Docker.ApiServices.StateMachines.FailedToStartState from LinuxWSL2
[16:02:33.557][EngineStateNotificationRecorder][Debug  ] Registered state {"State":"failed to start","Mode":"linux","date":1599634953}
[16:02:33.557][LoggingMessageHandler][Info   ] [26d81e32] <NotificationsClient start> POST http://localhost/engine/state
[16:02:33.558][SystrayNotifications][Error  ] Failed to set version to docker-desktop: exit code: -1
 stdout: ?[L?W0?0F0h0W0_0?d\Oo00?SgqW0_0?0?0?0?0?0?0n0.z^?g0o0?0?0?0?0U0?0f0D0~0[0?00
 
 stderr: 
[16:02:33.568][LoggingMessageHandler][Info   ] [26d81e32] <NotificationsClient end> POST http://localhost/engine/state -> 200 OK (took 11ms)
[16:02:33.589][Engines           ][Error  ] Start failed with Failed to set version to docker-desktop: exit code: -1
 stdout: ?[L?W0?0F0h0W0_0?d\Oo00?SgqW0_0?0?0?0?0?0?0n0.z^?g0o0?0?0?0?0U0?0f0D0~0[0?00
 
 stderr: 
   場所 Docker.Core.WslShortLivedCommandResult.LogAndThrowIfUnexpectedExitCode(String prefix, ILogger log, Int32 expectedExitCode) 場所 C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.Core\WslCommand.cs:行 119
   場所 Docker.Engines.WSL2.WSL2Provisioning.<ProvisionAsync>d__6.MoveNext() 場所 C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.Desktop\Engines\WSL2\WSL2Provisioning.cs:行 48
--- 直前に例外がスローされた場所からのスタック トレースの終わり ---
   場所 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   場所 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   場所 Docker.Engines.WSL2.LinuxWSL2Engine.<DoStartAsync>d__23.MoveNext() 場所 C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.Desktop\Engines\WSL2\LinuxWSL2Engine.cs:行 91
--- 直前に例外がスローされた場所からのスタック トレースの終わり ---
   場所 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   場所 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   場所 Docker.ApiServices.StateMachines.TaskExtensions.<WrapAsyncInCancellationException>d__0.MoveNext() 場所 C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.ApiServices\StateMachines\TaskExtensions.cs:行 29
--- 直前に例外がスローされた場所からのスタック トレースの終わり ---
   場所 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   場所 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   場所 Docker.ApiServices.StateMachines.StartTransition.<DoRunAsync>d__5.MoveNext() 場所 C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.ApiServices\StateMachines\StartTransition.cs:行 67
--- 直前に例外がスローされた場所からのスタック トレースの終わり ---
   場所 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   場所 Docker.ApiServices.StateMachines.StartTransition.<DoRunAsync>d__5.MoveNext() 場所 C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.ApiServices\StateMachines\StartTransition.cs:行 92
--- 直前に例外がスローされた場所からのスタック トレースの終わり ---
   場所 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   場所 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   場所 Docker.ApiServices.StateMachines.EngineStateMachine.<StartAsync>d__14.MoveNext() 場所 C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.ApiServices\StateMachines\EngineStateMachine.cs:行 72
--- 直前に例外がスローされた場所からのスタック トレースの終わり ---
   場所 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   場所 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   場所 Docker.Engines.Engines.<StartAsync>d__24.MoveNext() 場所 C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.Desktop\Engines\Engines.cs:行 109)
```