github.com/rish1988/moby@v25.0.2+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  	ocispec "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 *ocispec.Platform, containerName string) (container.CreateResponse, error) {
    24  	var response container.CreateResponse
    25  
    26  	// Make sure we negotiated (if the client is configured to do so),
    27  	// as code below contains API-version specific handling of options.
    28  	//
    29  	// Normally, version-negotiation (if enabled) would not happen until
    30  	// the API request is made.
    31  	cli.checkVersion(ctx)
    32  
    33  	if err := cli.NewVersionError(ctx, "1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
    34  		return response, err
    35  	}
    36  	if err := cli.NewVersionError(ctx, "1.41", "specify container image platform"); platform != nil && err != nil {
    37  		return response, err
    38  	}
    39  	if err := cli.NewVersionError(ctx, "1.44", "specify health-check start interval"); config != nil && config.Healthcheck != nil && config.Healthcheck.StartInterval != 0 && err != nil {
    40  		return response, err
    41  	}
    42  	if err := cli.NewVersionError(ctx, "1.44", "specify mac-address per network"); hasEndpointSpecificMacAddress(networkingConfig) && err != nil {
    43  		return response, err
    44  	}
    45  
    46  	if hostConfig != nil {
    47  		if versions.LessThan(cli.ClientVersion(), "1.25") {
    48  			// When using API 1.24 and under, the client is responsible for removing the container
    49  			hostConfig.AutoRemove = false
    50  		}
    51  		if versions.GreaterThanOrEqualTo(cli.ClientVersion(), "1.42") || versions.LessThan(cli.ClientVersion(), "1.40") {
    52  			// KernelMemory was added in API 1.40, and deprecated in API 1.42
    53  			hostConfig.KernelMemory = 0
    54  		}
    55  		if platform != nil && platform.OS == "linux" && versions.LessThan(cli.ClientVersion(), "1.42") {
    56  			// When using API under 1.42, the Linux daemon doesn't respect the ConsoleSize
    57  			hostConfig.ConsoleSize = [2]uint{0, 0}
    58  		}
    59  	}
    60  
    61  	// Since API 1.44, the container-wide MacAddress is deprecated and will trigger a WARNING if it's specified.
    62  	if versions.GreaterThanOrEqualTo(cli.ClientVersion(), "1.44") {
    63  		config.MacAddress = "" //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
    64  	}
    65  
    66  	query := url.Values{}
    67  	if p := formatPlatform(platform); p != "" {
    68  		query.Set("platform", p)
    69  	}
    70  
    71  	if containerName != "" {
    72  		query.Set("name", containerName)
    73  	}
    74  
    75  	body := configWrapper{
    76  		Config:           config,
    77  		HostConfig:       hostConfig,
    78  		NetworkingConfig: networkingConfig,
    79  	}
    80  
    81  	serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
    82  	defer ensureReaderClosed(serverResp)
    83  	if err != nil {
    84  		return response, err
    85  	}
    86  
    87  	err = json.NewDecoder(serverResp.body).Decode(&response)
    88  	return response, err
    89  }
    90  
    91  // formatPlatform returns a formatted string representing platform (e.g. linux/arm/v7).
    92  //
    93  // Similar to containerd's platforms.Format(), but does allow components to be
    94  // omitted (e.g. pass "architecture" only, without "os":
    95  // https://github.com/containerd/containerd/blob/v1.5.2/platforms/platforms.go#L243-L263
    96  func formatPlatform(platform *ocispec.Platform) string {
    97  	if platform == nil {
    98  		return ""
    99  	}
   100  	return path.Join(platform.OS, platform.Architecture, platform.Variant)
   101  }
   102  
   103  // hasEndpointSpecificMacAddress checks whether one of the endpoint in networkingConfig has a MacAddress defined.
   104  func hasEndpointSpecificMacAddress(networkingConfig *network.NetworkingConfig) bool {
   105  	if networkingConfig == nil {
   106  		return false
   107  	}
   108  	for _, endpoint := range networkingConfig.EndpointsConfig {
   109  		if endpoint.MacAddress != "" {
   110  			return true
   111  		}
   112  	}
   113  	return false
   114  }