WSL 2 and port forwarding

The most elegant way is not to play around with IP addresses. Let Docker do the work. A container can automatically access all LAN devices. Example (from WSL2):

$ docker container run --rm debian ping -c 1 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=37 time=2.26 ms

If this doesn’t work the traffic is probably blocked by your firewall.
To communicate between containers start them with a docker-compose file, they will automatically share a private network and can access each other using the service names.

1 Like