-
Notifications
You must be signed in to change notification settings - Fork 762
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
Add support for host.containers.internal in the /etc/hosts #3586
Conversation
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: rhatdan The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
host.containers.internal
has to point to the host ip and not the loopback ip.
if hostname != "" { | ||
hosts.Write([]byte(fmt.Sprintf("127.0.0.1 %s\n", hostname))) | ||
hosts.Write([]byte(fmt.Sprintf("::1 %s\n", hostname))) | ||
} | ||
|
||
// getLocalIP returns the non loopback local IP of the host | ||
getLocalIP := func() string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A host can validly have multiple network addresses, so this can return at most one address, or even no address. What does docker build
provide?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docker runs with in a network namespace with its own IP.
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
# docker build /tmp/test/
Sending build context to Docker daemon 2.048kB
Step 1/2 : from alpine
latest: Pulling from library/alpine
a0d0a0d46f8b: Pull complete
Digest: sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a
Status: Downloaded newer image for alpine:latest
---> 14119a10abf4
Step 2/2 : run cat /etc/hosts
---> Running in 0a834f95dd73
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 0a834f95dd73
Removing intermediate container 0a834f95dd73
---> ccb58e146963
Successfully built ccb58e146963
# docker run alpine cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 1adf4c1f38b5
Docker does not seem to have this internal hostname, although I thought we added this for compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Buildkit does:
#5 [2/3] RUN cat /etc/hosts
#5 0.346 127.0.0.1 localhost buildkitsandbox
#5 0.346 ::1 localhost ip6-localhost ip6-loopback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For docker you have to use --add-host=host.docker.internal:host-gateway
AFAIK and they use the default docker0 bridge ip address.
I think this solution is the best for buildah. For podman we use the CNI or slirp4netns gateway but this is also difficult to get.
run_linux.go
Outdated
} | ||
return "" | ||
} | ||
hosts.Write([]byte(fmt.Sprintf("%s %s\n", getLocalIP(), "host.containers.internal"))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should only be done when getLocalIP()
is not empty
All kinds of red test unhappiness @rhatdan |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
In the current version, |
Also switch default hostname from truncated ContainerID to Container name. It makes more sense that a user would know the name of the container versus the generated id, so we should use this as a default. Fixes: containers#3509 Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
@nalind Shouldn't this look like diff --git a/run_linux.go b/run_linux.go
index e0922c91..47e867fc 100644
--- a/run_linux.go
+++ b/run_linux.go
@@ -717,10 +717,9 @@ func (b *Builder) generateHosts(rdir, hostname string, addHosts []string, chownO
hosts.Write([]byte(fmt.Sprintf("%s\t%s\n", values[1], values[0])))
}
- if hostname != "" {
- hosts.Write([]byte(fmt.Sprintf("127.0.0.1 %s\n", hostname)))
- hosts.Write([]byte(fmt.Sprintf("::1 %s\n", hostname)))
- }
+ hosts.Write([]byte(fmt.Sprintf("127.0.0.1 localhost %s %s\n", b.Container, hostname)))
+ hosts.Write([]byte(fmt.Sprintf("::1 localhost %s %s\n", b.Container, hostname)))
+ |
LGTM |
Just one doubt since we are removing check for Since I am not sure why this check was there in the first place. |
The check was there in case their was no entry. You would have had "::1 " |
/hold |
return "" | ||
} | ||
for _, address := range addrs { | ||
// check the address type and if it is not a loopback the display it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
// check the address type and if it is not a loopback the display it | |
// check the address type and if it is not a loopback then return it |
LGTM |
Thanks got it. |
/hold cancel |
Also switch default hostname from truncated ContainerID to Container
name.
It makes more sense that a user would know the name of the container
versus the generated id, so we should use this as a default.
Fixes: #3509
Signed-off-by: Daniel J Walsh dwalsh@redhat.com
What type of PR is this?
What this PR does / why we need it:
How to verify it
Which issue(s) this PR fixes:
Special notes for your reviewer:
Does this PR introduce a user-facing change?