container_create.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "path"
  7. "github.com/docker/docker/api/types/container"
  8. "github.com/docker/docker/api/types/network"
  9. "github.com/docker/docker/api/types/versions"
  10. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  11. )
  12. type configWrapper struct {
  13. *container.Config
  14. HostConfig *container.HostConfig
  15. NetworkingConfig *network.NetworkingConfig
  16. }
  17. // ContainerCreate creates a new container based on the given configuration.
  18. // It can be associated with a name, but it's not mandatory.
  19. func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error) {
  20. var response container.CreateResponse
  21. // Make sure we negotiated (if the client is configured to do so),
  22. // as code below contains API-version specific handling of options.
  23. //
  24. // Normally, version-negotiation (if enabled) would not happen until
  25. // the API request is made.
  26. cli.checkVersion(ctx)
  27. if err := cli.NewVersionError(ctx, "1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
  28. return response, err
  29. }
  30. if err := cli.NewVersionError(ctx, "1.41", "specify container image platform"); platform != nil && err != nil {
  31. return response, err
  32. }
  33. if err := cli.NewVersionError(ctx, "1.44", "specify health-check start interval"); config != nil && config.Healthcheck != nil && config.Healthcheck.StartInterval != 0 && err != nil {
  34. return response, err
  35. }
  36. if err := cli.NewVersionError(ctx, "1.44", "specify mac-address per network"); hasEndpointSpecificMacAddress(networkingConfig) && err != nil {
  37. return response, err
  38. }
  39. if hostConfig != nil {
  40. if versions.LessThan(cli.ClientVersion(), "1.25") {
  41. // When using API 1.24 and under, the client is responsible for removing the container
  42. hostConfig.AutoRemove = false
  43. }
  44. if versions.GreaterThanOrEqualTo(cli.ClientVersion(), "1.42") || versions.LessThan(cli.ClientVersion(), "1.40") {
  45. // KernelMemory was added in API 1.40, and deprecated in API 1.42
  46. hostConfig.KernelMemory = 0
  47. }
  48. if platform != nil && platform.OS == "linux" && versions.LessThan(cli.ClientVersion(), "1.42") {
  49. // When using API under 1.42, the Linux daemon doesn't respect the ConsoleSize
  50. hostConfig.ConsoleSize = [2]uint{0, 0}
  51. }
  52. }
  53. // Since API 1.44, the container-wide MacAddress is deprecated and will trigger a WARNING if it's specified.
  54. if versions.GreaterThanOrEqualTo(cli.ClientVersion(), "1.44") {
  55. config.MacAddress = "" //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
  56. }
  57. query := url.Values{}
  58. if p := formatPlatform(platform); p != "" {
  59. query.Set("platform", p)
  60. }
  61. if containerName != "" {
  62. query.Set("name", containerName)
  63. }
  64. body := configWrapper{
  65. Config: config,
  66. HostConfig: hostConfig,
  67. NetworkingConfig: networkingConfig,
  68. }
  69. serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
  70. defer ensureReaderClosed(serverResp)
  71. if err != nil {
  72. return response, err
  73. }
  74. err = json.NewDecoder(serverResp.body).Decode(&response)
  75. return response, err
  76. }
  77. // formatPlatform returns a formatted string representing platform (e.g. linux/arm/v7).
  78. //
  79. // Similar to containerd's platforms.Format(), but does allow components to be
  80. // omitted (e.g. pass "architecture" only, without "os":
  81. // https://github.com/containerd/containerd/blob/v1.5.2/platforms/platforms.go#L243-L263
  82. func formatPlatform(platform *ocispec.Platform) string {
  83. if platform == nil {
  84. return ""
  85. }
  86. return path.Join(platform.OS, platform.Architecture, platform.Variant)
  87. }
  88. // hasEndpointSpecificMacAddress checks whether one of the endpoint in networkingConfig has a MacAddress defined.
  89. func hasEndpointSpecificMacAddress(networkingConfig *network.NetworkingConfig) bool {
  90. if networkingConfig == nil {
  91. return false
  92. }
  93. for _, endpoint := range networkingConfig.EndpointsConfig {
  94. if endpoint.MacAddress != "" {
  95. return true
  96. }
  97. }
  98. return false
  99. }