client/container_create.go
7c36a1af
 package client
 
 import (
 	"encoding/json"
 	"net/url"
 	"strings"
 
 	"github.com/docker/docker/api/types/container"
 	"github.com/docker/docker/api/types/network"
0ea7b143
 	"github.com/docker/docker/api/types/versions"
7c36a1af
 	"golang.org/x/net/context"
 )
 
 type configWrapper struct {
 	*container.Config
 	HostConfig       *container.HostConfig
 	NetworkingConfig *network.NetworkingConfig
 }
 
 // ContainerCreate creates a new container based in the given configuration.
 // It can be associated with a name, but it's not mandatory.
bad849fc
 func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) {
 	var response container.ContainerCreateCreatedBody
e98e4a71
 
 	if err := cli.NewVersionError("1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
 		return response, err
 	}
 
0ea7b143
 	// When using API 1.24 and under, the client is responsible for removing the container
 	if hostConfig != nil && versions.LessThan(cli.ClientVersion(), "1.25") {
 		hostConfig.AutoRemove = false
 	}
 
7c36a1af
 	query := url.Values{}
 	if containerName != "" {
 		query.Set("name", containerName)
 	}
 
 	body := configWrapper{
 		Config:           config,
 		HostConfig:       hostConfig,
 		NetworkingConfig: networkingConfig,
 	}
 
 	serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
 	if err != nil {
 		if serverResp.statusCode == 404 && strings.Contains(err.Error(), "No such image") {
81bb9978
 			return response, objectNotFoundError{object: "image", id: config.Image}
7c36a1af
 		}
 		return response, err
 	}
 
 	err = json.NewDecoder(serverResp.body).Decode(&response)
 	ensureReaderClosed(serverResp)
 	return response, err
 }