github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/compat/containers_export.go (about) 1 package compat 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 "os" 7 8 "github.com/containers/podman/v2/libpod" 9 "github.com/containers/podman/v2/pkg/api/handlers/utils" 10 "github.com/pkg/errors" 11 ) 12 13 func ExportContainer(w http.ResponseWriter, r *http.Request) { 14 runtime := r.Context().Value("runtime").(*libpod.Runtime) 15 name := utils.GetName(r) 16 con, err := runtime.LookupContainer(name) 17 if err != nil { 18 utils.ContainerNotFound(w, name, err) 19 return 20 } 21 tmpfile, err := ioutil.TempFile("", "api.tar") 22 if err != nil { 23 utils.Error(w, "unable to create tarball tempfile", http.StatusInternalServerError, errors.Wrap(err, "unable to create tempfile")) 24 return 25 } 26 defer os.Remove(tmpfile.Name()) 27 if err := tmpfile.Close(); err != nil { 28 utils.Error(w, "unable to close tempfile", http.StatusInternalServerError, errors.Wrap(err, "unable to close tempfile")) 29 return 30 } 31 if err := con.Export(tmpfile.Name()); err != nil { 32 utils.Error(w, "failed to save the image", http.StatusInternalServerError, errors.Wrap(err, "failed to save image")) 33 return 34 } 35 rdr, err := os.Open(tmpfile.Name()) 36 if err != nil { 37 utils.Error(w, "failed to read temp tarball", http.StatusInternalServerError, errors.Wrap(err, "failed to read the exported tarfile")) 38 return 39 } 40 defer rdr.Close() 41 utils.WriteResponse(w, http.StatusOK, rdr) 42 }