github.com/rawahars/moby@v24.0.4+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 if err := cli.NewVersionError("1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil { 27 return response, err 28 } 29 if err := cli.NewVersionError("1.41", "specify container image platform"); platform != nil && err != nil { 30 return response, err 31 } 32 33 if hostConfig != nil { 34 if versions.LessThan(cli.ClientVersion(), "1.25") { 35 // When using API 1.24 and under, the client is responsible for removing the container 36 hostConfig.AutoRemove = false 37 } 38 if versions.GreaterThanOrEqualTo(cli.ClientVersion(), "1.42") || versions.LessThan(cli.ClientVersion(), "1.40") { 39 // KernelMemory was added in API 1.40, and deprecated in API 1.42 40 hostConfig.KernelMemory = 0 41 } 42 if platform != nil && platform.OS == "linux" && versions.LessThan(cli.ClientVersion(), "1.42") { 43 // When using API under 1.42, the Linux daemon doesn't respect the ConsoleSize 44 hostConfig.ConsoleSize = [2]uint{0, 0} 45 } 46 } 47 48 query := url.Values{} 49 if p := formatPlatform(platform); p != "" { 50 query.Set("platform", p) 51 } 52 53 if containerName != "" { 54 query.Set("name", containerName) 55 } 56 57 body := configWrapper{ 58 Config: config, 59 HostConfig: hostConfig, 60 NetworkingConfig: networkingConfig, 61 } 62 63 serverResp, err := cli.post(ctx, "/containers/create", query, body, nil) 64 defer ensureReaderClosed(serverResp) 65 if err != nil { 66 return response, err 67 } 68 69 err = json.NewDecoder(serverResp.body).Decode(&response) 70 return response, err 71 } 72 73 // formatPlatform returns a formatted string representing platform (e.g. linux/arm/v7). 74 // 75 // Similar to containerd's platforms.Format(), but does allow components to be 76 // omitted (e.g. pass "architecture" only, without "os": 77 // https://github.com/containerd/containerd/blob/v1.5.2/platforms/platforms.go#L243-L263 78 func formatPlatform(platform *ocispec.Platform) string { 79 if platform == nil { 80 return "" 81 } 82 return path.Join(platform.OS, platform.Architecture, platform.Variant) 83 }