Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create host.rancher-desktop.internal and host.docker.internal aliases #1000

Merged
merged 1 commit into from
Nov 25, 2021

Conversation

jandubois
Copy link
Member

as symbolic names to access a service running on the host from inside the VM.

Implements #893 for Linux and macOS

lima-rancher-desktop:/Users/jan$ ping host.rancher-desktop.internal
PING host.rancher-desktop.internal (192.168.5.2): 56 data bytes
64 bytes from 192.168.5.2: seq=0 ttl=42 time=0.159 ms
64 bytes from 192.168.5.2: seq=1 ttl=42 time=0.335 ms
64 bytes from 192.168.5.2: seq=2 ttl=42 time=0.128 ms
^C

as symbolic names to access a service running on the host from inside the VM.

Signed-off-by: Jan Dubois <[email protected]>
mode: system
script: |
#!/bin/sh
sed -i 's/host.lima.internal.*/host.lima.internal host.rancher-desktop.internal host.docker.internal/' /etc/hosts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we capture the rest of the line in a \(.*\) group and echo it out? Since the line to change comes from the lima-vm repo seems like we should be more flexible to changes there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we capture the rest of the line in a (.*) group and echo it out?

No, I'm intentionally discarding it. The changes we make are persisted, so we would append the additional names over and over on each reboot otherwise.

If Lima ever adds more than one name to that line, then we have to adapt. But most likely Lima will generate additional lines if/when it wants to define more aliases for the gateway address...

Copy link
Contributor

@ericpromislow ericpromislow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with a note about possibly dropping entries.

@ericpromislow ericpromislow merged commit 21167c0 into main Nov 25, 2021
@ericpromislow ericpromislow deleted the host.docker.internal branch November 25, 2021 22:54
@jandubois jandubois added this to the v0.7.0 milestone Nov 25, 2021
@jandubois jandubois added the kind/enhancement New feature or request label Nov 25, 2021
@djfoxer
Copy link

djfoxer commented Dec 3, 2021

@jandubois Hi! What about Windows? Is host.docker.internal alias will ba available also for Rancher Desktop for Windows?

@jandubois
Copy link
Member Author

@djfoxer Not yet; that is tracked in #893. It is not quite clear yet which address to pick on WSL2, so very likely will not make it into the upcoming release.

@djfoxer
Copy link

djfoxer commented Dec 3, 2021

@jandubois I see. Maybe my code will be useful somehow. I have small workaround for Windows (powershell script):

[updated]

$ip = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $(Get-NetConnectionProfile | Select-Object -ExpandProperty InterfaceIndex) | Select-Object -ExpandProperty IPAddress;
$command = "nerdctl exec container_name  /bin/sh -c `"echo " + $ip + " host.docker.internal >> /etc/hosts`"";
invoke-expression $command

This tiny script takes IP from Windows and add one line to the end of /etc/hosts file in container to translate host.docker.internal into Windows IP. Works for me 😃

@jandubois
Copy link
Member Author

Maybe my code will be useful somehow.

@djfoxer Thank you for sharing! I've linked to it from the other issue, so whoever will work on it sees it.

@djfoxer
Copy link

djfoxer commented Dec 3, 2021

@jandubois Great! I will track this issue.

@dougbreaux
Copy link

Hmmm... what's different about my version of PowerShell and yours, @djfoxer ? First I got that "var keyword is not supported", then some missing line terminator stuff, presumably because of the kinds of quotes used. Still trying to work that out...

@dougbreaux
Copy link

dougbreaux commented Jan 15, 2022

First command also doesn't seem to produce the same IPs that

wsl -d rancher-desktop ip route

does.

Ahh, this does:

Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias "vEthernet (WSL)" | Select-Object -ExpandProperty IPAddress

@djfoxer
Copy link

djfoxer commented Jan 16, 2022

@dougbreaux

Hmmm... what's different about my version of PowerShell and yours, @djfoxer ? First I got that "var keyword is not supported", then some missing line terminator stuff, presumably because of the kinds of quotes used. Still trying to work that out...

Sorry, I probably copied wrong code, my current version:

$ip = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $(Get-NetConnectionProfile | Select-Object -ExpandProperty InterfaceIndex) | Select-Object -ExpandProperty IPAddress;
$command = "nerdctl exec container_name  /bin/sh -c `"echo " + $ip + " host.docker.internal >> /etc/hosts`"";
invoke-expression $command

I will update my comment.

@smileart
Copy link

When I SSH to lima supplied with Rancher Desktop, I indeed see all the hosts in /etc/hosts, but the containers I'm running in it apparently know nothing about such hosts and their respective /etc/hosts are pretty default. Do I miss something? Cause reaching the host MacOS machine from lima is one thing, but letting containers to do the same, that's what I'd expect from this mechanism as it is with host.docker.internal in Docker Desktop. 🤔

@jandubois
Copy link
Member Author

Do I miss something?

No, this is not working because the built-in resolver in Lima doesn't run inside the VM, it runs on the host to be able to resolve names from VPN etc. This will be fixed by lima-vm/lima#622 (so that Rancher Desktop can add these static names to the lima config).

A workaround would be to add the settings to /etc/hosts on the macOS machine:

192.168.5.2	host.lima.internal host.rancher-desktop.internal host.docker.internal

The names will only work inside the VM and the containers, not on the host. But the 192.168.5.2 IP address is fixed by qemu/lima, so this should work fine (on macOS only).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants