Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download image when starting a container using the compat API #12315

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions pkg/api/handlers/compat/containers_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/containers/common/pkg/config"
"github.com/containers/podman/v3/cmd/podman/common"
"github.com/containers/podman/v3/libpod"
"github.com/containers/podman/v3/pkg/api/handlers"
Expand Down Expand Up @@ -54,13 +55,20 @@ func CreateContainer(w http.ResponseWriter, r *http.Request) {

newImage, resolvedName, err := runtime.LibimageRuntime().LookupImage(body.Config.Image, nil)
if err != nil {
if errors.Cause(err) == storage.ErrImageUnknown {
utils.Error(w, "No such image", http.StatusNotFound, err)
if errors.Cause(err) != storage.ErrImageUnknown {
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "error looking up image"))
return
}
_, err = runtime.LibimageRuntime().Pull(r.Context(), body.Config.Image, config.PullPolicyAlways, nil)
if err != nil {
utils.Error(w, "No such image", http.StatusNotFound, errors.Wrap(err, "Cannot pull image"))
return
}
newImage, resolvedName, err = runtime.LibimageRuntime().LookupImage(body.Config.Image, nil)
if err != nil {
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "error looking up image after pulling"))
return
}

utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "error looking up image"))
return
}

// Take body structure and convert to cliopts
Expand Down
16 changes: 16 additions & 0 deletions test/python/docker/compat/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,19 @@ def test_non_existant_workdir(self):
ctr.start()
ret, out = ctr.exec_run(["stat", "/workspace/scratch/test"])
self.assertEqual(ret, 0, "Working directory created if it doesn't exist")

def test_non_local_image(self):
self.assertEqual(len(self.client.images.list(filters={"reference": "alpine"})), 1)

for c in self.client.containers.list(all=True):
c.remove(force=True)

self.client.images.remove(constant.ALPINE)

self.assertEqual(len(self.client.images.list(filters={"reference": "alpine"})), 0)

ctr: Container = self.client.containers.create(image=constant.ALPINE, detach=True,
name="non_local", command="top")
ctr.start()

self.assertEqual(len(self.client.images.list(filters={"reference": "alpine"})), 1)