Skip to content

Commit

Permalink
Add nohosts option to /build and /libpod/build
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin Lam <[email protected]>
  • Loading branch information
gavinkflam committed Nov 18, 2024
1 parent 6680d56 commit c791acf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/api/handlers/compat/images_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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),
Expand Down
12 changes: 12 additions & 0 deletions pkg/api/server/register_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions pkg/bindings/images/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
25 changes: 25 additions & 0 deletions test/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit c791acf

Please sign in to comment.