Windows port 莫名其妙被占用

在使用 Windows OS 做開發的時候,常常莫名其妙要開 docker container 的時候發生錯誤,看錯誤訊息是 port 被占用。但我才剛開機啥都還沒開怎麼會被占用? 於是 Google 了一下發現知名的 Will保哥有寫了一個詳細的紀錄。 感謝保哥!

分享

在使用 Windows OS 做開發的時候,常常莫名其妙要開 docker container 的時候發生錯誤,看錯誤訊息是 port 被占用。但我才剛開機啥都還沒開怎麼會被占用? 於是 Google 了一下發現知名的 Will保哥有寫了一個詳細的紀錄。
感謝保哥!

為避免我未來忘記,趕快來記錄一下。
想要看比較完整個資訊可以直接到下方連結:Will保哥的網站

資料來源:

Windows 10 無法 LISTEN Port 4200 與 Port 3000 的靈異事件整理

查看問題

首先使用 netstat 發現沒有看到自己的 port 有被其他程式占用netstat -nat | findstr [port]

但 docker container 啟動失敗是事實,所以其中一定出了什麼問題

用這個指令可以看目前 tcp port 被排除的範圍
沒錯!我的 port 就在被排除使用的範圍內!netsh int ipv4 show excludedportrange protocol=tcp


解決方法# 1. 停用 WSL 服務
wsl --shutdown# 2. 停用 Docker 服務  
net stop winnat# 3. 把自己常用的 port 設定保留# 增加
netsh int ipv4 add excludedportrange protocol=tcp numberofports=1 startport=[port]# 刪除
netsh int ipv4 delete excludedportrange protocol=tcp numberofports=1 startport=3000# 4. 重啟 WinNAT 服務
net start winnat

走完上面的流程後就可以成功的啟動