container_create.go 3.2 KB

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