From d18369ee8804543bfad15e93e3210466416f7f70 Mon Sep 17 00:00:00 2001 From: Urvashi Mohnani Date: Mon, 2 Oct 2023 15:05:18 -0400 Subject: [PATCH] Check for image with /libpod/containers/create The libpod containers create endpoint wasn't checking whether the image existed before creating the container. If the image doesn't exist, it should return a 404 status code but it was failing and returning a 500 status code. This fix matches the behavior of the compat endpoint. Signed-off-by: Urvashi Mohnani --- pkg/api/handlers/libpod/containers_create.go | 10 ++++++++++ test/apiv2/20-containers.at | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/pkg/api/handlers/libpod/containers_create.go b/pkg/api/handlers/libpod/containers_create.go index dc12d76ae3..28f2ff5dbe 100644 --- a/pkg/api/handlers/libpod/containers_create.go +++ b/pkg/api/handlers/libpod/containers_create.go @@ -2,6 +2,7 @@ package libpod import ( "encoding/json" + "errors" "fmt" "net/http" "strconv" @@ -13,6 +14,7 @@ import ( "github.com/containers/podman/v4/pkg/specgen" "github.com/containers/podman/v4/pkg/specgen/generate" "github.com/containers/podman/v4/pkg/specgenutil" + "github.com/containers/storage" ) // CreateContainer takes a specgenerator and makes a container. It returns @@ -60,12 +62,20 @@ func CreateContainer(w http.ResponseWriter, r *http.Request) { warn, err := generate.CompleteSpec(r.Context(), runtime, &sg) if err != nil { + if errors.Is(err, storage.ErrImageUnknown) { + utils.Error(w, http.StatusNotFound, fmt.Errorf("no such image: %w", err)) + return + } utils.InternalServerError(w, err) return } rtSpec, spec, opts, err := generate.MakeContainer(r.Context(), runtime, &sg, false, nil) if err != nil { + if errors.Is(err, storage.ErrImageUnknown) { + utils.Error(w, http.StatusNotFound, fmt.Errorf("no such image: %w", err)) + return + } utils.InternalServerError(w, err) return } diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index 6906273fe9..31d1b48e4a 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -174,6 +174,10 @@ if root; then podman rm -f $CTRNAME fi +# Container create without existing image should return 404 +t POST libpod/containers/create Image="foo" 404 \ + .cause="image not known" + # Issue #6799: it should be possible to start a container, even w/o args. t POST libpod/containers/create?name=test_noargs Image=${IMAGE} 201 \ .Id~[0-9a-f]\\{64\\}