github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/api/handlers/libpod/containers_create.go (about) 1 package libpod 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 "github.com/containers/libpod/libpod" 8 "github.com/containers/libpod/pkg/api/handlers/utils" 9 "github.com/containers/libpod/pkg/specgen" 10 "github.com/containers/libpod/pkg/specgen/generate" 11 "github.com/pkg/errors" 12 ) 13 14 // CreateContainer takes a specgenerator and makes a container. It returns 15 // the new container ID on success along with any warnings. 16 func CreateContainer(w http.ResponseWriter, r *http.Request) { 17 runtime := r.Context().Value("runtime").(*libpod.Runtime) 18 var sg specgen.SpecGenerator 19 if err := json.NewDecoder(r.Body).Decode(&sg); err != nil { 20 utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "Decode()")) 21 return 22 } 23 if err := generate.CompleteSpec(r.Context(), runtime, &sg); err != nil { 24 utils.InternalServerError(w, err) 25 return 26 } 27 ctr, err := generate.MakeContainer(runtime, &sg) 28 if err != nil { 29 utils.InternalServerError(w, err) 30 return 31 } 32 response := utils.ContainerCreateResponse{ID: ctr.ID()} 33 utils.WriteJSON(w, http.StatusCreated, response) 34 }