1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「VPNに繋がないとlocalhostが見れない」WSL2×Dockerの奇妙なバグに終止符を打つまでの記録

1
Last updated at Posted at 2026-05-08

悲しいことにlocalhostが壊れてしまいました。
VPNに繋がないとlocalhostにアクセスできない謎の現象に悩まされてました。

image.png

発生するバグの再現手順と症状

以下の手順を踏むと、確定でDockerのlocalhostが壊れます。(わざと壊して検証済み)

  1. .wslconfig で networkingMode=mirrored がオンになっている。
  2. この状態で docker compose up 等でコンテナを起動する。
  3. Windows側でL2TP VPNをオンにする。
  4. その後、L2TP VPNをオフにする。

その結果VPNオフ状態では、localhostと127.0.0.1にアクセスが不可能になります。

解決策:mirrored networking を無効化して再起動する

このバグに遭遇した場合、.wslconfig の mirrored設定をオフにしてNATに戻し、再起動するのが一番簡単です。

手順1. .wslconfig の修正

Windows側の C:\Users\<ユーザー名>\.wslconfig を開き、mirrored をコメントアウトしましょう

[wsl2]
# networkingMode=mirrored

手順2. PCを再起動

設定を変更したら、Windowsを再起動してください。
VS Code や Docker Desktop が裏で WSL を掴んだままになっていると設定が完全に反映されないため、PCごと再起動してしまうのが最も確実で手っ取り早いです。

手順3. コンテナの再起動

再起動後、Docker Desktop が立ち上がったら、再度コンテナを起動します。

docker compose up -d --build

これで、VPNのオンオフに関わらず localhost 経由のアクセスが正常に行えるようになります。

まとめ

  • WSL2 の mirrored モード有効時に L2TP VPN のオン/オフを行うと、docker経由のlocalhostが完全に壊れる。
  • 壊れるとVPNオフ状態ではlocalhost(v4/v6両方) にアクセス不能になる。
  • 確実な解決策は networkingMode=mirrored をやめて、素直にPCを再起動すること。

Docker環境でVPN使用後に突然ローカルサーバーに繋がらなくなった方は、まず.wslconfigの設定を疑ってみてください。
ちなみに、mirroredオプションをオフの状態でL2TP VPNのオンオフを繰り返してもlocalhostが壊れることはありませんでした。
mirroredオプション、WindowsとWSLのIPを統一できて便利なんですけどまだ不安定ですね...

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?