Skip to content

Allow adding static names to hostresolver #650

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

Merged
merged 3 commits into from
Feb 18, 2022

Conversation

jandubois
Copy link
Member

@jandubois jandubois commented Feb 15, 2022

This is an alternative to adding static names to /etc/hosts on the host, to make them available inside containers running in the guest. In addition this change allows creating aliases to host.lima.internal to point to the host gateway from inside the guest.

Manual testing with examples/docker.yaml with these changes:

hostResolver:
  hosts:
    host.docker.internal: host.lima.internal
    cname.docker.internal: host.docker.internal
    guest.internal: 127.1.1.1

Testing inside an Alpine container running on Docker:

$ docker context create lima --docker "host=unix:///Users/jan/.lima/docker/sock/docker.sock"
$ docker context use lima
$ docker run -it --rm alpine
/ # nslookup host.docker.internal
Server:		10.0.2.3
Address:	10.0.2.3:53

Non-authoritative answer:

Non-authoritative answer:
host.docker.internal	canonical name = host.lima.internal
Name:	host.lima.internal
Address: 192.168.5.2

/ # nslookup cname.docker.internal
Server:		10.0.2.3
Address:	10.0.2.3:53

Non-authoritative answer:

Non-authoritative answer:
cname.docker.internal	canonical name = host.lima.internal
Name:	host.lima.internal
Address: 192.168.5.2

/ # nslookup guest.internal
Server:		10.0.2.3
Address:	10.0.2.3:53

Non-authoritative answer:
Name:	guest.internal
Address: 127.1.1.1

Non-authoritative answer:

In addition this PR contains a bugfix for the hostResolver.ipv6: false configuration: instead of forwarding AAAA queries to the default resolvers, we now return an empty reply to avoid returning NXDOMAIN. During testing I found that that was causing names to resolve correctly on Alpine even when dig and nslookup seemed to show proper DNS information. It might be a bug in the musl resolver.

Finally this PR also adds the current hostname with the slirp IP address to the host resolver. This is done automatically by systemd-resolved, but necessary for e.g. Alpine.

Fixes #622

Forwarding the request to the defaultHandler could result in returning
NXDOMAIN, which can break resolution of IPv4 addresses.

Signed-off-by: Jan Dubois <[email protected]>
@jandubois jandubois added this to the v0.8.3 milestone Feb 15, 2022
@AkihiroSuda AkihiroSuda added enhancement New feature or request impact/changelog labels Feb 15, 2022
@jandubois jandubois force-pushed the hosts branch 2 times, most recently from d6b2f68 to 41f0d54 Compare February 15, 2022 09:00
@AkihiroSuda
Copy link
Member

Seems accidentally containing #643

Could you try rebasing with the master?

@jandubois
Copy link
Member Author

Could you try rebasing with the master?

Should be already fixed; maybe you need to refresh?

@jandubois
Copy link
Member Author

Sorry, I did one more force-push to change the hostname address from 127.0.1.1 to 192.168.5.15 (the slirp IP address). This seems to be more in line with the Debian hostname resolution documentation, given that the slirp IP is pretty permanent:

The IP address 127.0.1.1 in the second line of this example may not be found on some other Unix-like systems. The Debian Installer creates this entry for a system without a permanent IP address as a workaround for some software (e.g., GNOME) as documented in the bug #719621.

@@ -56,6 +52,9 @@ probes:
exit 1
fi
hint: See "/var/log/cloud-init-output.log". in the guest
hostResolver:
hosts:
host.docker.internal: host.lima.internal
portForwards:
- guestSocket: "/run/user/{{.UID}}/docker.sock"
Copy link
Member

Choose a reason for hiding this comment

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

Can we split this yaml to another PR and hold it until right before the release of vNext, to avoid seeing “this example does not work” issues?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've restored the original script because I realized it is still required when hostResolver.enabled is false.

I've added comments to the file that should make it obvious that this feature requires lima 0.8.3 or later.

I've also added tests to the PR.

This is an alternative to adding them globally to /etc/hosts on the
host (not the guest). It also allows aliasing them to host.lima.internal.

This allows names to be resolved not just inside the guest, but also inside
containers inside the guest, which only have access to /etc/resolv.conf, and
not the full resolver inside the guest.

Signed-off-by: Jan Dubois <[email protected]>
This is done automatically by systemd-resolved, in which case this
change is ignored, but on e.g. Alpine this is required to connect
to the VM using its own hostname.

Signed-off-by: Jan Dubois <[email protected]>
Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Thanks

@AkihiroSuda AkihiroSuda merged commit 7397425 into lima-vm:master Feb 18, 2022
@jandubois jandubois deleted the hosts branch February 18, 2022 05:26
@jandubois
Copy link
Member Author

@abiosoft You may want to look at the changes to examples/docker.yaml and copy the hostResolver.hosts setting. It will be ignored in older versions of lima, but with 0.8.3 it will make sure that host.docker.internal can be resolved inside containers as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow adding static names to hostresolver
2 participants