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

Add support for host.containers.internal in the /etc/hosts #3586

Merged
merged 1 commit into from
Oct 14, 2021

Conversation

rhatdan
Copy link
Member

@rhatdan rhatdan commented Oct 12, 2021

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?

/kind api-change
/kind bug
/kind cleanup
/kind deprecation
/kind design
/kind documentation
/kind failing-test
/kind feature
/kind flake
/kind other

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?


@openshift-ci
Copy link
Contributor

openshift-ci bot commented Oct 12, 2021

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@rhatdan
Copy link
Member Author

rhatdan commented Oct 12, 2021

tests/run.bats Outdated Show resolved Hide resolved
Copy link
Member

@Luap99 Luap99 left a 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 {
Copy link
Member Author

Choose a reason for hiding this comment

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

@nalind @Luap99 do we have any better way then this to get the hosts ip?

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

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

Copy link
Member

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")))
Copy link
Member

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

@TomSweeneyRedHat
Copy link
Member

All kinds of red test unhappiness @rhatdan

@rhatdan
Copy link
Member Author

rhatdan commented Oct 14, 2021

Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

LGTM

@nalind
Copy link
Member

nalind commented Oct 14, 2021

In the current version, generateHosts() only writes entries for 127.0.0.1 and ::1 if hostname is set, which looks like an error if it's also trying to add b.Container as a name for those addresses.

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>
@flouthoc
Copy link
Collaborator

In the current version, generateHosts() only writes entries for 127.0.0.1 and ::1 if hostname is set, which looks like an error if it's also trying to add b.Container as a name for those addresses.

@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)))
+

@flouthoc
Copy link
Collaborator

LGTM

@flouthoc
Copy link
Collaborator

Just one doubt since we are removing check for if hostname != "" should we still have a line to set localhost if hostname is actually empty.

Since I am not sure why this check was there in the first place.

@rhatdan
Copy link
Member Author

rhatdan commented Oct 14, 2021

The check was there in case their was no entry. You would have had "::1 "
Now we are at least guaranteed to have container name.

@rhatdan
Copy link
Member Author

rhatdan commented Oct 14, 2021

/hold

return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
Copy link
Member

Choose a reason for hiding this comment

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

nit

Suggested change
// 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

@TomSweeneyRedHat
Copy link
Member

LGTM

@flouthoc
Copy link
Collaborator

The check was there in case their was no entry. You would have had "::1 " Now we are at least guaranteed to have container name.

Thanks got it.

@rhatdan
Copy link
Member Author

rhatdan commented Oct 14, 2021

/hold cancel

@openshift-merge-robot openshift-merge-robot merged commit e4a4f2c into containers:main Oct 14, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include "host.containers.internal" entry during the image build process
6 participants