16

For Docker Desktop inside a container, the DNS name host.docker.internal resolves to an IP address allowing network access to the host. Is there something similar when using Rancher Desktop?

Assuming a running container (e.g the alpine image) in Docker Desktop it's possible to run

docker exec alpine-container ping -c 2 host.docker.internal

Update: This has been resolved and released with v1.0.1 as it seems.

Andi
  • 650
  • 2
  • 8
  • 22

3 Answers3

8

When using the dockerd (moby) container runtime in Rancher Desktop, adding --add-host=host.docker.internal:host-gateway to your docker run command will achieve this behavior.

docker run -it --add-host=host.docker.internal:host-gateway alpine cat /etc/hosts

Or for docker-compose:

# docker-compose.yml
  my_app:
    image: ...
    extra_hosts:
      - "host.docker.internal:host-gateway"

https://megamorf.gitlab.io/2020/09/19/access-native-services-on-docker-host-via-host-docker-internal/#implementation

However I don't believe this will work when using Rancher Desktop with containerd as the container runtime.

Eric B
  • 691
  • 5
  • 11
  • 3
    This solution works, however it requires an invasive change of the artifacts such as docker-compose yaml files, thus effectively creating a difference with Docker Desktop. It would be useful to have this as an option in the Rancher Desktop settings – Mihai Ungureanu Nov 14 '22 at 10:58
5

As mentioned in the question, this has been resolved in Rancher Desktop. However, for Windows users who might hit this question, a firewall rule might be required. https://docs.rancherdesktop.io/faq/

The following rule might be required according to the faq.

New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow
Jaap
  • 695
  • 1
  • 6
  • 10
wejoey
  • 216
  • 1
  • 3
  • 14
2

There is an open GitHub issue for it [0]. As a workaround until this is closed, a hint from a member of Rancher Labs might help (at least when running Rancher Desktop on Linux/macOS):

I don't think we have a symbolic name for it, but the host is always (on Linux and macOS) accessible via 192.168.5.2, which is the qemu gateway address. [1]

[0] https://github.com/rancher-sandbox/rancher-desktop/issues/1316

[1] https://rancher-users.slack.com/archives/C0200L1N1MM/p1634568974296000?thread_ts=1634560787.294400&cid=C0200L1N1MM

Andi
  • 650
  • 2
  • 8
  • 22