github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/container_create.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"net/url"
     7  
     8  	"github.com/containerd/containerd/platforms"
     9  	"github.com/docker/docker/api/types/container"
    10  	"github.com/docker/docker/api/types/network"
    11  	"github.com/docker/docker/api/types/versions"
    12  	specs "github.com/opencontainers/image-spec/specs-go/v1"
    13  )
    14  
    15  type configWrapper struct {
    16  	*container.Config
    17  	HostConfig       *container.HostConfig
    18  	NetworkingConfig *network.NetworkingConfig
    19  	Platform         *specs.Platform
    20  }
    21  
    22  // ContainerCreate creates a new container based in the given configuration.
    23  // It can be associated with a name, but it's not mandatory.
    24  func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *specs.Platform, containerName string) (container.ContainerCreateCreatedBody, error) {
    25  	var response container.ContainerCreateCreatedBody
    26  
    27  	if err := cli.NewVersionError("1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
    28  		return response, err
    29  	}
    30  
    31  	// When using API 1.24 and under, the client is responsible for removing the container
    32  	if hostConfig != nil && versions.LessThan(cli.ClientVersion(), "1.25") {
    33  		hostConfig.AutoRemove = false
    34  	}
    35  
    36  	if err := cli.NewVersionError("1.41", "specify container image platform"); platform != nil && err != nil {
    37  		return response, err
    38  	}
    39  
    40  	query := url.Values{}
    41  	if platform != nil {
    42  		query.Set("platform", platforms.Format(*platform))
    43  	}
    44  
    45  	if containerName != "" {
    46  		query.Set("name", containerName)
    47  	}
    48  
    49  	body := configWrapper{
    50  		Config:           config,
    51  		HostConfig:       hostConfig,
    52  		NetworkingConfig: networkingConfig,
    53  	}
    54  
    55  	serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
    56  	defer ensureReaderClosed(serverResp)
    57  	if err != nil {
    58  		return response, err
    59  	}
    60  
    61  	err = json.NewDecoder(serverResp.body).Decode(&response)
    62  	return response, err
    63  }