Docker Community Forums

Share and learn in the Docker community.

WSL 2 and port forwarding

Hi there, I am running a docker container on WSL 2 which I want to connect to from my LAN.
The docker container gets by default ip 172.17.0.x, if I define manually an ip (such as “–ip 192.168.3.100” my LAN) the containers cannot communicate with my lan devices.
I was thinking to create a port forwarding between the host (Windows 10) and the container.
Is there a more elegant way to accomplish that? Note that I will also need other containers to talk each others.

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
docker run -d -p 192.168.3.100:80:80 docker/getting-started

map your IP alias and port 80 of the host to port 80 in the container

-p 192.168.3.100:80:80