github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/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  	"github.com/docker/docker/api/types/versions"
    11  	"golang.org/x/net/context"
    12  )
    13  
    14  type configWrapper struct {
    15  	*container.Config
    16  	HostConfig       *container.HostConfig
    17  	NetworkingConfig *network.NetworkingConfig
    18  }
    19  
    20  // ContainerCreate creates a new container based in the given configuration.
    21  // It can be associated with a name, but it's not mandatory.
    22  func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) {
    23  	var response container.ContainerCreateCreatedBody
    24  
    25  	if err := cli.NewVersionError("1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
    26  		return response, err
    27  	}
    28  
    29  	// When using API 1.24 and under, the client is responsible for removing the container
    30  	if hostConfig != nil && versions.LessThan(cli.ClientVersion(), "1.25") {
    31  		hostConfig.AutoRemove = false
    32  	}
    33  
    34  	query := url.Values{}
    35  	if containerName != "" {
    36  		query.Set("name", containerName)
    37  	}
    38  
    39  	body := configWrapper{
    40  		Config:           config,
    41  		HostConfig:       hostConfig,
    42  		NetworkingConfig: networkingConfig,
    43  	}
    44  
    45  	serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
    46  	if err != nil {
    47  		if serverResp.statusCode == 404 && strings.Contains(err.Error(), "No such image") {
    48  			return response, objectNotFoundError{object: "image", id: config.Image}
    49  		}
    50  		return response, err
    51  	}
    52  
    53  	err = json.NewDecoder(serverResp.body).Decode(&response)
    54  	ensureReaderClosed(serverResp)
    55  	return response, err
    56  }