WSL: WSL 2.0: `networkingMode=mirrored` makes Docker unable to forward ports
Windows Version
Microsoft Windows [Version 10.0.22631.2338]
WSL Version
2.0.0.0
Are you using WSL 1 or WSL 2?
- WSL 2
Kernel Version
5.15.123.1-1
Distro Version
Ubuntu 20.04.6
Other Software
Docker version 24.0.6, build ed223bc
Repro Steps
- Change
networkingModetomirrored docker run -d -p 8080:80 nginx:alpine(example)- Go to
localhost:8080 - Cannot connect
- Try from WSL
curl http//localhost:8080same issue
Expected Behavior
To forward the port and be able to connect to my containers
Actual Behavior
Doesn’t forward the port, so I cannot connect to my containers.
Diagnostic Logs
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 89
- Comments: 171 (16 by maintainers)
there seem to be two issues why Docker containers cannot connect from Windows.
ignorePortsworks Docker Desktop, probablythe port forwarding does work from another machine on the same network as host. Just not on the host machine itself.docker-ce that installed on Linuxtemporary measures for
2…disable iptables and use docker-proxy/etc/docker/daemon.json
when using mirrored, the behavior seems to be different from the previous localhostforwarding.
use docker-proxy(listen on Linux)
interface is different, but the behavior remains the same.
use iptables(listeon on container)
via localhostforwarding(until), source address(Windows) was the docker network gateway (=pointing to linux).
via mirrored, source address is 127.0.0.1. for this reason that packet is returned to 127.0.0.1(localhost on container) and does not reach Windows. in the case of access from another node(PC), there is no problem because source is his address.
Interestingly, the port forwarding does work from another machine on the same network as host. Just not on the host machine itself.
Machine A:
Machine B:
This issue should not be closed, the issue is still there if you’re not using docker desktop. Iptables is more stable then userland proxy
I’m closing this issue out as it’s now fixed! Please upgrade to docker desktop 4.26.0 to get the fix. Thank you all! 😃
So the two workarounds I see so far, both have their negatives but worth listing anyway. Correct any of my misunderstandings.
Option1 - without docker desktop
Edit
%USERPROFILE%\.wslconfigfile (create it, if doesn’t exist) and add this sectionRestart WSL, then in your distro, edit
/etc/docker/daemon.jsonand addThen
sudo systemctl restart docker.With this, both normal port usage like
python3 -m http.serverand docker port usage likedocker run -p "8080:8080" --rm -t mendhak/http-https-echo:26are accessible from Windows side. The disadvantage is that iptables is now disabled.Option 2 - with Docker Desktop
No longer necessary, update to the latest Docker Desktop and it should start working.
Edit
%USERPROFILE%\.wslconfigfile (create it, if doesn’t exist) and add this sectionRestart WSL and ensure Docker Desktop is running. With this, the normal port usage stops working if it uses a port listed above. eg the Python http.server isn’t accessible over 8000 anymore (at least in my testing). Instead use a different port, like
python3 -m http.server 8001. Docker ports will work as normal as long as the port is listed in theignoredPortsabove, likedocker run -p "8080:8080" --rm -t mendhak/http-https-echo:26The disadvantage here is you have to know which ports you’ll be using with Docker. And it doesn’t look like
ignoredPortsaccepts ranges of ports either so it can get pretty tedious.And ignoredPorts only applies to docker containers it seems, not simple python http servers, unless I messed up? Did anyone run into the ignoredPorts issue as I did, for non-Docker applications?
hello. Docker completed the PR that fixes this issue - https://github.com/moby/moby/pull/48514 This was included in the following Docker release - https://github.com/moby/moby/releases/tag/v27.3.0
Please try Docker version 27.3.0
Same issue here, the only way I found to make it “work” was adding ignoredPorts=8080 to the wslconfig, but if container has something like 4800:8080 need to add both ports to
ignoredPorts=8080,4800still doing this some containers might not work at all, also if cloudflare tunnel is running on WSL this will break to when mirrored and theres no way to know which ports is using to add it to theignoredPortsclosing since the issue is resolved if you encounter the issue after using Docker version 27.3.0 or later, please open a separate issue
添加下面配置到wslconfig
并且添加
"iptables": false到/etc/docker/daemon.json就好了Hi folks, we believe we’ve found a fix for this that needs to be added to the Docker engine. We’ve filed a bug for it and are working with the docker team to investigate adding it in: https://github.com/moby/moby/issues/48056
ignorePortsis misleading. What it does is making in this case port8080available to request inside the container only, but is not forwarded to Windows side. Look for more details at https://learn.microsoft.com/en-us/windows/wsl/wsl-configIf you run
get-nettcpConnection -LocalPort 8080there’s nothing listening on that port.After starting the web server with
docker run -d -p 8080:80 nginx:alpinerequests inside the WSL container works:Requests from Windows side timeout with:
curl: (28) Failed to connect to localhost port 8080 after 21276 ms: Couldn't connect to serverFrom another server it work, as mentioned by @driver1998 :
My favorite flavor of Linux is Ubuntu via Docker via Ubuntu via WSL via Windows 11.
To follow-up on this thread, we are actively working to address this. We are considering solution such as that shown by @shigenobuokamoto earlier in this thread. We are currently considering options and possible side-effects for the multitude of ways Linux might be configured by users.
Thanks everyone for the detailed feedback!
Docker Desktop 4.26.0 Release Notes For Windows * Added support for WSL mirrored mode networking (requires WSL v2.0.4 and up). * Added missing signatures on DLL and VBS files.
+1 for priority. There is a large contingent of corporate devs who need docker and mirrored mode (vpn/sec reqs).
ignoredPorts really isn’t an ideal solution. Hopefully we can get an official update on this item as it’s already quite popular.
@CatalinFetoiu it may not be practical to modify the nft rules set by Docker every time. instead, if you run the following two lines, traffic from the Windows host will be handled by 127.0.0.1 (= docker-proxy).
i have been using this rule for a long time and it does not seem to have any side effects.
Network Mode mirrored does not work for me with docker. Looks like port 2375 is only reachable via IPv6 (
netstat - nl | grep 2375only returns a mapping for ‘tcp6’).Why I can reach http://localhost:2375/version via Browser, e. g. Testcontainers does not find a docker environment (despite DOCKER_HOST set to ‘tcp://localhost:2375’).
Even after 8 years, docker is just a nightmare with WSL(2).I am literally the only dev using Windows anymore on my team, everyone else is using Macs now where it just works.
@felipecrs
most issues can be resolved by modifying daemon.json. however, the usability may change by not using iptables. bigger issues is that cannot use docker unless setting daemon.json. my script is just a sample implementation workaround, but if it has a reasonable effect without any negative effects on everyone, i hope that i will be able to propose its inclusion in
/init.https://gist.github.com/shigenobuokamoto/b565d468541fc8be7d7d76a0434496a0
my script is very simple, just apply the following rules to nftables.
this chain is processed immediately before PREROUTING, and will DNAT destination of the packet that arrives from loopback0 interface to 127.0.0.1 to 127.0.0.1. it may seem like a wasteful process, but adding this action breaks the prerouting hook and disables any Docker rules set in the PREROUTING chain.
using userland-proxy only for access from localhost is the same as Docker’s default behavior.
to: who have already used it i have made some changes from the previous one,
both are methods to prevent the PREROUTING NAT rule. there is no change in behavior when using with Docker, but i think this is safer because it does not affect the chain managed by iptables.
if you like, please try this as well.
Thank you for posting @AzureZeng
Would appreciate a few more people checking to confirm, but I just did a quick test and it seems to be working better than before, hopefully no other issues appear 🤞
In
%USERPROFILE%\.wslconfigI only put this and nothing else:and updated to Docker Desktop 4.26.0
I am now able to
and also
Both are visible from the Windows side in a browser.
Right: some apps may want to connect to an assigned address instead of loopback. In those cases, it might help those solutions end-to-end to also enable hostAddressLoopback
(hostAddressLoopback is only applicable to Mirrored mode)
@LeorGreenberger i cannot reproduce it. however, there were some parts where the escape was missing, so I fixed it. could you please try this?
https://gist.github.com/shigenobuokamoto/b565d468541fc8be7d7d76a0434496a0
Wow, I couldn’t get it to work in mirrored mode for 4 hours, then it randomly worked after restarting services, killing processes, restarting wsl, running docker desktop as admin etc. The next day I couldn’t open docker desktop, the engine would just not start, for 2 hours.
Modifying my config finally worked:
I added:
so my config now looks like this:
even if iptables:true, unless you explicitly set userland-proxy: false, access from localhost will go through userlandproxy.
workaround if you don’t want to set iptables:false https://gist.github.com/shigenobuokamoto/b565d468541fc8be7d7d76a0434496a0
i would like microsoft to consider the issues with docker-ce (!= Docker Desktop).
@craigloewen-msft, any words for the people who are not using Docker Desktop (which is not even free)? Is
echo '{"iptables": false}' > /etc/docker/daemon.jsonthe proper approach?Thanks for the work around. We are engaged with the Docker team to have better integration.
I tested with disable iptables for docker in
/etc/docker/daemon.jsonand usedocker-proxyinstead. It works on my device.Another thing, when I set
autoMemoryReclaimtogradual, all containers won’t start. So I have to set it todropcache.My working
.wslconfigMy
/etc/docker/daemon.jsoncontent:@zaaack, you can use
iptables -t nat -I DOCKER -i loopback0 -d 127.0.0.0/8 -j RETURN¹ or any of the variants² that have been mentioned above.Also, this would mean that whatever application that relies on the same feature as docker would have the same issue. It could be an old application that doesn’t receive more support for example. Or some application that refuses to implement a fix just for WSL.
So, yeah, I totally agree. The ideal would have been the fix to happen on WSL side.
This issue seems to have been resolved for me with WSL 2.2.4.0. Can anyone else confirm this so we can close the issue?
Thank you @shigenobuokamoto! That did the trick! Since, unfortunately, I do not understand what your script does… I am not able to explain why it works!
Ok, that’s cool, but I hope this issue does not get closed already. What about people who are not using Docker Desktop? (which is not free and neither open source)
Sorry, it was close for internal tracking of issues. If it’s more appropriate, we can keep this open as well.
It works in my test. 😃
@jweaston you can access from Docker container in WSL to Windows host address, WSL 2.3.11 and later.
.wslconfig
docker container (in WSL) can connect to 192.168.1.10:5966. if you try connect to 127.0.0.1:5966, the packet is sent to container’s 127.0.0.1. so cannot connect to windows host.
When will this fix be built into wsl? Now we can only use service script to make it work, I don’t think this is a long term solution.
Besides, I use nixos-wsl, this is my example module that can be used as a reference for nixos user, from @shigenobuokamoto
@codeart1st work on this issue is ongoing in moby (=Docker Engine), so until that is released you can use my script to mitigate the issue. essentially, the issue is that network within WSL behaves differently from normal Linux, so i think it would be best to deal with it on the WSL side.
WSL 2.3.11 includes some improvements to mirrored networking, so i am looking into how to use them to improve the experience. this is the prototype new network-mirrored.service. please try this too.
https://gist.github.com/shigenobuokamoto/540c5f09a03eb07149501e99a6c8d82b
@shigenobuokamoto’s solution continues to be rock-solid over many months of 10h/day usage. Write yourself a personal VM setup guide and just copy paste their stuff.
https://gist.github.com/shigenobuokamoto/b565d468541fc8be7d7d76a0434496a0
how about this?
it works for me,but this causes me to not be able to use iptables and I’m still waiting for a real fix. Also, I’ve heard that Docker Desktop 4.26 and later adds support for mirrored network mode.
This is still an issue.
I always find the solution right after it’s been fixed. So lucky.
See above, set
networkingMode=mirroredandhostAddressLoopback=truein.wslconfig, then set"iptables": falsein/etc/docker/daemon.json.I think there is not more convenient solution than this, unless Docker team want to improve the experience that running Docker daemon directly in WSL.
this might be closer to the previous behavior. (instead daemon.json)
generally, packets from local loopback are not routed. when mirrored, PREROUTING chain works because it routes packets from loopback0. so let the packets from loopback0 escape from PREROUTING chain.
however, this requires additional configuration. if WSL has set rules for nftables in advance… loopback0.service.gz
@keith-horton Why is this being closed when you are still actively working with Docker? Is there another issue that I can track? Is this workaround an official workaround suggested by MS? 😃 Are there any negative/downsides from turning iptables off in Docker w/ WSL?
@keith-horton brother, you just changed my life. thank you
this may seem unnecessary since moby takes care of it, but this is a new improvement plan.
WSL 2.3.11 brings several improvements to communication with Windows host.
therefore, you can now use the following rules to improve communication from Windows host to Docker containers:
@felipecrs : right - 2.3.1.1 fixes a variety of Linux container scenarios. This in particular will require Docker to change their route - tracked here: https://github.com/moby/moby/issues/48056.
We are in contact with them and the fix should hopefully be released soon.
seems still have side efforts:
above bullet 4 in WSL2 NAT workable.
More interestingly… it only works when I have wireshark doing a local loopback capture.
I’m really not feeling like going deeper into win32 the c++ behind npcap… but this is enough c# code to open the loopback device… you dont even have to start a capture just open the device handle and the connection starts working.
as soon as the process with the above code is ended the machine goes back to not functioning.
hello, thanks for your patience while we investigated this
we are talking with Docker to address this in their product, but in the meantime, you can run these commands to update the Docker nftables rules to allow localhost port forwarding to work
step 1) Run “sudo nft -a list chain ip nat DOCKER”. Example output shown below. table ip nat { chain DOCKER { # handle 4 iifname “docker0” counter packets 0 bytes 0 return # handle 7 iifname != “docker0” meta l4proto tcp tcp dport 8080 counter packets 0 bytes 0 dnat to 172.17.0.2:80 # handle 12 } }
step 2) using the handle of the second rule in the DOCKER chain from step 1 (the rule that includes the “dnat” keyword), run the following command: (please make sure to use the handle from your output in step 1, as the handle number can change)
sudo nft -a delete rule ip nat DOCKER handle 12
step 3) Run the following command. Make sure that the ports and IP address match your output from step 1 (in this case they need to be the same as the rule with handle 12 from step 1)
sudo nft add rule ip nat DOCKER iifname != “docker0” iifname != “loopback0” meta l4proto tcp tcp dport 8080 counter dnat to 172.17.0.2:80
I keep coming back to this and @shigenobuokamoto’s solution is the only thing that works for me.
Waiting for WSL to fix this issue.
Yea something is out of order. Corporate firewall wouldn’t prevent you from accessing WSL on your own computer.
Anyway, add this to your WSL OS (https://github.com/microsoft/WSL/issues/10494#issuecomment-1951848642) and add the following settings to your .wslconfig. It’s similar to what you have but with additional flags. This is working fine across all of my Win11 PCs
Still an issue with v4.28.0
Although
networkingMode=mirroredis great, I hope it does not get promoted to default without having this issue resolved first. 😅@shigenobuokamoto Your fix works!!! I ran your service start command as root at the command line. Not tested the stop service yet. Our use case is a windows fat client connected to a backend composed of a bunch of containers started with docker compose in WSL2.
You saved my life!
@shigenobuokamoto This works! I reloaded, stopped and started a bunch of times and I don’t see any escape errors. On stopping the service, I get the following warning, but I am guessing this is expected:
This worked!!
edit: Spoke too soon. @shigenobuokamoto Seeing this error:
Works perfectly when I run the script manually in the shell. Think systemd wants something escaped.
Just to update, this issue is still present with WSL
2.1.1.0and docker25.0.2.None of these workaround works. Just change back to NAT and waiting for fix.
Im running docker
24.0.7with docker desktop version4.26.1and i’m still experiencing the issue:https://github.com/microsoft/WSL/assets/6466560/cb659445-dc55-45a9-ac1a-d9b4388bf8bd
Not sure if i’m doing something wrong but it feels very much not solved 😂 happy to help diagnose 😉
Yes, I’m aware of that and that’s what I am using. BTW, I don’t need to set
hostAddressLoopback=trueand apparently it still works fine.But I do expect some answer from either the WSL team or Docker as to if this is the recommended/final approach or not.
Does that mean a fix has been developed and we’re just waiting on a new version? If so, awesome! Thank you!
@caaat233 it works indeed, thanks a lot! All it takes is to put
{"iptables": false}.