github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+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  	"path"
     8  
     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  }
    20  
    21  // ContainerCreate creates a new container based on the given configuration.
    22  // It can be associated with a name, but it's not mandatory.
    23  func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *specs.Platform, containerName string) (container.ContainerCreateCreatedBody, error) {
    24  	var response container.ContainerCreateCreatedBody
    25  
    26  	if err := cli.NewVersionError("1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
    27  		return response, err
    28  	}
    29  
    30  	// When using API 1.24 and under, the client is responsible for removing the container
    31  	if hostConfig != nil && versions.LessThan(cli.ClientVersion(), "1.25") {
    32  		hostConfig.AutoRemove = false
    33  	}
    34  
    35  	if err := cli.NewVersionError("1.41", "specify container image platform"); platform != nil && err != nil {
    36  		return response, err
    37  	}
    38  
    39  	query := url.Values{}
    40  	if p := formatPlatform(platform); p != "" {
    41  		query.Set("platform", p)
    42  	}
    43  
    44  	if containerName != "" {
    45  		query.Set("name", containerName)
    46  	}
    47  
    48  	body := configWrapper{
    49  		Config:           config,
    50  		HostConfig:       hostConfig,
    51  		NetworkingConfig: networkingConfig,
    52  	}
    53  
    54  	serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
    55  	defer ensureReaderClosed(serverResp)
    56  	if err != nil {
    57  		return response, err
    58  	}
    59  
    60  	err = json.NewDecoder(serverResp.body).Decode(&response)
    61  	return response, err
    62  }
    63  
    64  // formatPlatform returns a formatted string representing platform (e.g. linux/arm/v7).
    65  //
    66  // Similar to containerd's platforms.Format(), but does allow components to be
    67  // omitted (e.g. pass "architecture" only, without "os":
    68  // https://github.com/containerd/containerd/blob/v1.5.2/platforms/platforms.go#L243-L263
    69  func formatPlatform(platform *specs.Platform) string {
    70  	if platform == nil {
    71  		return ""
    72  	}
    73  	return path.Join(platform.OS, platform.Architecture, platform.Variant)
    74  }