Error response from daemon: Ports are not available: listen tcp 0.0.0.0:8888: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

在 WSL2 使用 docker 時出現以下錯誤:

Error response from daemon: Ports are not available: listen tcp 0.0.0.0:8888: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

經查,應該是  windows 的 default dynamic port range for TCP/IP 被改變了,參考 The default dynamic port range for TCP/IP has changed in Windows Vista and in Windows Server 2008 - Windows Server | Microsoft Docs

輸入

netsh interface ipv4 show excludedportrange protocol=tcp

會顯示被 exclude port range

  1583        1682
  1683        1782
  2480        2579
  4492        4591
  5357        5357
  5614        5713
  5834        5933
  5940        6039
  6045        6144
  6276        6375
  6491        6590
  6897        6996
  7003        7102
 28385       28385
 50000       50059     *

參考下文,重設新的 range

docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:4449: bind: An attempt was made to access a socket in a way forbidden by its access permissions. · Issue #9272 · docker/for-win
I have tried with the latest version of my channel (Stable or Edge) I have uploaded Diagnostics Diagnostics ID: Expected behavior Actual behavior Information Windows Version: Docker Desktop Version...

Changing the dynamic port range with netsh int ipv4 set dynamic tcp start=49152 num=16384 (source) and after a restart excluded port ranges are ok.