How to resolve mdns hostname of host from Docker Desktop in wsl?

I use Docker Desktop 4.39 in wsl2. I try to resolve my mdns names of local network. I can do it successfully except hostname of host machine with docker itself.

If I understand correct, I see in wsl2 this output

$ ping HostHostname
127.0.1.1

And in docker container

$ ping HostHostname
ping: bad address "HostHostname"

So looks like that 127.0.1.1 address is redirected to 127.0.0.11 docker resolver, but 127.0.1.1 address is omitted on low level because this is just about host hostname.

I’ve found a way out to run container with docker --hostname HostHostname. But this is not the best way because I will not be able to achieve the container by its name in docker inner networks between containers.

So, are any better ways to resolve a problem?

That is just set in your hosts file (/etc/hosts) which will not be set in containers running in another WSL distribution (docker-desktop) and since loopback IPs are pointing back to their own network namespace, and containers have their own (not to mention the different WSL distribution), it wouldn’t work from containers even if you got the same loopback IP from a container.

Yes, maybe I should mention that I used this existing Ubuntu wsl2 where I did this ping via Resources → Wsl Integration

Also I had marked Enable host networking

Do I understand correct that this should resolve problem?

-v /etc/hosts:/etc/hosts

Or what are simplest and good ways that you recommend?

/etc/hosts is automatically mounted from the host, not part of the image filesystem. Of course it will mount one only from inside the virtual machine, but if you want to add host IP mappings, you can add extra_hosts in compose or use --add-host with docker run.

hmm, it is interesting. So, should I do something like that docker run --add-host HostHostname=127.0.1.1?

Definitely not. As I wrote before

Use the IP of the host

Can you give me example what ip you mean?

I don’t need to get host. I need just loopback the request if recipient address will be the same as hostname

Then it looks like I still don’t understand the issue. Why would you try to set a loopback IP address to the hostname of the host when using the hostname from a container? But if it was your intention, then yes, use the loopback IP what you quoted in your message and ignore what I wrote before. If you test it and it works as intended, then it is what you need to do.

I can do upstreams in nginx, for example, that contain all servers in my local network. And I can want to put the same nginx on a few machines. It will be problematically to set “localhost” or “host.docker.internal” for each server in nginx upstream where it matches with current machine mdns domain name.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.