From d6140690d3f312cdd65068cd7ff3cc5266b800a3 Mon Sep 17 00:00:00 2001 From: Gavin Lam Date: Sun, 17 Nov 2024 01:32:32 -0500 Subject: [PATCH] Add nohosts option to /build and /libpod/build Signed-off-by: Gavin Lam --- pkg/api/handlers/compat/images_build.go | 2 ++ pkg/api/server/register_images.go | 12 ++++++++++++ pkg/bindings/images/build.go | 3 +++ test/e2e/build_test.go | 25 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/pkg/api/handlers/compat/images_build.go b/pkg/api/handlers/compat/images_build.go index 004f241ef1..e5a5e5a91b 100644 --- a/pkg/api/handlers/compat/images_build.go +++ b/pkg/api/handlers/compat/images_build.go @@ -142,6 +142,7 @@ func BuildImage(w http.ResponseWriter, r *http.Request) { Memory int64 `schema:"memory"` NamespaceOptions string `schema:"nsoptions"` NoCache bool `schema:"nocache"` + NoHosts bool `schema:"nohosts"` OmitHistory bool `schema:"omithistory"` OSFeatures []string `schema:"osfeature"` OSVersion string `schema:"osversion"` @@ -720,6 +721,7 @@ func BuildImage(w http.ResponseWriter, r *http.Request) { LabelOpts: labelOpts, Memory: query.Memory, MemorySwap: query.MemSwap, + NoHosts: query.NoHosts, OmitHistory: query.OmitHistory, SeccompProfilePath: seccomp, ShmSize: strconv.Itoa(query.ShmSize), diff --git a/pkg/api/server/register_images.go b/pkg/api/server/register_images.go index d9247ac67c..2cb79470ae 100644 --- a/pkg/api/server/register_images.go +++ b/pkg/api/server/register_images.go @@ -523,6 +523,12 @@ func (s *APIServer) registerImagesHandlers(r *mux.Router) error { // TBD Extra hosts to add to /etc/hosts // (As of version 1.xx) // - in: query + // name: nohosts + // type: boolean + // default: + // description: | + // Not to create /etc/hosts when building the image + // - in: query // name: remote // type: string // default: @@ -1502,6 +1508,12 @@ func (s *APIServer) registerImagesHandlers(r *mux.Router) error { // TBD Extra hosts to add to /etc/hosts // (As of version 1.xx) // - in: query + // name: nohosts + // type: boolean + // default: + // description: | + // Not to create /etc/hosts when building the image + // - in: query // name: remote // type: string // default: diff --git a/pkg/bindings/images/build.go b/pkg/bindings/images/build.go index 11ba58c6d4..e9f689959d 100644 --- a/pkg/bindings/images/build.go +++ b/pkg/bindings/images/build.go @@ -335,6 +335,9 @@ func Build(ctx context.Context, containerFiles []string, options types.BuildOpti if options.NoCache { params.Set("nocache", "1") } + if options.CommonBuildOpts.NoHosts { + params.Set("nohosts", "1") + } if t := options.Output; len(t) > 0 { params.Set("output", t) } diff --git a/test/e2e/build_test.go b/test/e2e/build_test.go index 234a2d6802..fe7ef8f10c 100644 --- a/test/e2e/build_test.go +++ b/test/e2e/build_test.go @@ -737,6 +737,31 @@ RUN [[ -L /test/dummy-symlink ]] && echo SYMLNKOK || echo SYMLNKERR`, CITEST_IMA Expect(session.OutputToString()).To(ContainSubstring("SYMLNKOK")) }) + It("podman build --no-hosts", func() { + targetPath := podmanTest.TempDir + + containerFile := filepath.Join(targetPath, "Containerfile") + content := `FROM scratch +RUN echo '56.78.12.34 image.example.com' > /etc/hosts` + + Expect(os.WriteFile(containerFile, []byte(content), 0755)).To(Succeed()) + + defer func() { + Expect(os.RemoveAll(containerFile)).To(Succeed()) + }() + + session := podmanTest.Podman([]string{ + "build", "--pull-never", "-t", "hosts_test", "--no-hosts", "--from", CITEST_IMAGE, targetPath, + }) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + + session = podmanTest.Podman([]string{"run", "--no-hosts", "--rm", "hosts_test", "cat", "/etc/hosts"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + Expect(session.OutputToString()).To(Equal("56.78.12.34 image.example.com")) + }) + It("podman build --from, --add-host, --cap-drop, --cap-add", func() { targetPath := podmanTest.TempDir