github.com/brandon-bethke-neudesic/moby@v1.13.1/client/container_create.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"github.com/docker/docker/api/types/container"
     9  	"github.com/docker/docker/api/types/network"
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  type configWrapper struct {
    14  	*container.Config
    15  	HostConfig       *container.HostConfig
    16  	NetworkingConfig *network.NetworkingConfig
    17  }
    18  
    19  // ContainerCreate creates a new container based in the given configuration.
    20  // It can be associated with a name, but it's not mandatory.
    21  func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) {
    22  	var response container.ContainerCreateCreatedBody
    23  
    24  	if err := cli.NewVersionError("1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
    25  		return response, err
    26  	}
    27  
    28  	query := url.Values{}
    29  	if containerName != "" {
    30  		query.Set("name", containerName)
    31  	}
    32  
    33  	body := configWrapper{
    34  		Config:           config,
    35  		HostConfig:       hostConfig,
    36  		NetworkingConfig: networkingConfig,
    37  	}
    38  
    39  	serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
    40  	if err != nil {
    41  		if serverResp.statusCode == 404 && strings.Contains(err.Error(), "No such image") {
    42  			return response, imageNotFoundError{config.Image}
    43  		}
    44  		return response, err
    45  	}
    46  
    47  	err = json.NewDecoder(serverResp.body).Decode(&response)
    48  	ensureReaderClosed(serverResp)
    49  	return response, err
    50  }