github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/libpod/containers_create.go (about)

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