Skip to content

Commit

Permalink
Use hosts public ip address in rootless containers
Browse files Browse the repository at this point in the history
Add first non localhost ipv4 of all host interfaces as destination
for host.contaners.internal for rootless containers.

Fixes: containers#12000

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Dec 6, 2021
1 parent b1ca2e6 commit c02b87a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
7 changes: 7 additions & 0 deletions cmd/podman/images/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"

buildahDefine "github.com/containers/buildah/define"
Expand Down Expand Up @@ -221,6 +223,11 @@ func build(cmd *cobra.Command, args []string) error {
// The context directory could be a URL. Try to handle that.
tempDir, subDir, err := buildahDefine.TempDirForURL("", "buildah", args[0])
if err != nil {
if ee, ok := (errors.Cause(err)).(*exec.ExitError); ok {
if w, ok := ee.Sys().(syscall.WaitStatus); ok {
registry.SetExitCode(w.ExitStatus())
}
}
return errors.Wrapf(err, "error prepping temporary context directory")
}
if tempDir != "" {
Expand Down
12 changes: 8 additions & 4 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2159,11 +2159,15 @@ func (c *Container) getHosts() string {
}
}
} else if c.config.NetMode.IsSlirp4netns() {
gatewayIP, err := GetSlirp4netnsGateway(c.slirp4netnsSubnet)
if err != nil {
logrus.Warn("Failed to determine gatewayIP: ", err.Error())
if ip := butil.LocalIP(); ip != "" {
hosts += fmt.Sprintf("%s\t%s\n", ip, "host.containers.internal")
} else {
hosts += fmt.Sprintf("%s host.containers.internal\n", gatewayIP.String())
gatewayIP, err := GetSlirp4netnsGateway(c.slirp4netnsSubnet)
if err != nil {
logrus.Warn("Failed to determine gatewayIP: ", err.Error())
} else {
hosts += fmt.Sprintf("%s host.containers.internal\n", gatewayIP.String())
}
}
} else {
logrus.Debug("Network configuration does not support host.containers.internal address")
Expand Down
13 changes: 10 additions & 3 deletions pkg/api/handlers/compat/images_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -492,8 +493,8 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
reporter := channel.NewWriter(make(chan []byte))
defer reporter.Close()

runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
rtc, err := runtime.GetConfig()
rt := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
rtc, err := rt.GetConfig()
if err != nil {
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "Decode()"))
return
Expand Down Expand Up @@ -568,6 +569,12 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
utils.BadRequest(w, "platform", platformSpec, err)
return
}
if os == "" {
os = runtime.GOOS
}
if arch == "" {
arch = runtime.GOARCH
}
buildOptions.Platforms = append(buildOptions.Platforms, struct{ OS, Arch, Variant string }{
OS: os,
Arch: arch,
Expand All @@ -587,7 +594,7 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
runCtx, cancel := context.WithCancel(context.Background())
go func() {
defer cancel()
imageID, _, err = runtime.Build(r.Context(), buildOptions, containerFiles...)
imageID, _, err = rt.Build(r.Context(), buildOptions, containerFiles...)
if err == nil {
success = true
} else {
Expand Down
3 changes: 2 additions & 1 deletion test/system/500-networking.bats
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ load helpers

@test "podman run with slirp4ns assigns correct addresses to /etc/hosts" {
CIDR="$(random_rfc1918_subnet)"
IP=$(hostname -I | cut -f 1 -d " ")
local conname=con-$(random_string 10)
run_podman run --rm --network slirp4netns:cidr="${CIDR}.0/24" \
--name $conname --hostname $conname $IMAGE cat /etc/hosts
is "$output" ".*${CIDR}.2 host.containers.internal" "host.containers.internal should be the cidr+2 address"
is "$output" ".*${IP} host.containers.internal" "host.containers.internal should be the first host ipv4 address"
is "$output" ".*${CIDR}.100 $conname $conname" "$conname should be the cidr+100 address"
}

Expand Down

0 comments on commit c02b87a

Please sign in to comment.