From 030b0be9d824ce9e6fb56acfbaa2af081a9e2973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Rod=C3=A1k?= Date: Fri, 10 Jul 2026 14:59:51 +0200 Subject: [PATCH] Add `HostIp()` method to `InspectHostPort` for Docker template compat MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docker-compatible inspect templates using `{{.HostIp}}` fail in Podman because Go templates resolve by struct field name (`HostIP`), not JSON tag (`HostIp`). Add a `HostIp()` method so both notations work. Note: this does not address the struct-vs-map difference that prevents `range` over individual port bindings (would require an API break). Relates: https://github.com/podman-container-tools/podman/issues/29164 Signed-off-by: Jan Rodák --- libpod/define/container_inspect.go | 6 ++++++ test/e2e/container_inspect_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/libpod/define/container_inspect.go b/libpod/define/container_inspect.go index 51ad4a0da7f..03da50a536c 100644 --- a/libpod/define/container_inspect.go +++ b/libpod/define/container_inspect.go @@ -265,6 +265,12 @@ type InspectHostPort struct { HostPort string `json:"HostPort"` } +// HostIp allows Docker-compatible {{.HostIp}} access in Go templates. +// See https://github.com/containers/podman/issues/29164 +func (hp InspectHostPort) HostIp() string { + return hp.HostIP +} + // InspectMount provides a record of a single mount in a container. It contains // fields for both named and normal volumes. Only user-specified volumes will be // included, and tmpfs volumes are not included even if the user specified them. diff --git a/test/e2e/container_inspect_test.go b/test/e2e/container_inspect_test.go index 0abca8e442b..107af4e6e84 100644 --- a/test/e2e/container_inspect_test.go +++ b/test/e2e/container_inspect_test.go @@ -107,6 +107,19 @@ var _ = Describe("Podman container inspect", func() { Expect(data[0].Config.Annotations[define.VolumesFromAnnotation]).To(Equal(volsctr)) }) + // https://github.com/containers/podman/issues/29164 + It("podman inspect supports docker-compatible HostIp in format template", func() { + ctrName := "hostip-compat" + session := podmanTest.Podman([]string{"create", "--name", ctrName, "-p", "127.0.0.1::8080/tcp", ALPINE}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + + inspect := podmanTest.Podman([]string{"inspect", "--format", `{{ (index (index .NetworkSettings.Ports "8080/tcp") 0).HostIp }}`, ctrName}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).Should(ExitCleanly()) + Expect(inspect.OutputToString()).To(Equal("127.0.0.1")) + }) + It("podman inspect hides secrets mounted to env", func() { secretName := "mysecret"