container_create.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. if err := cli.NewVersionError("1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
  22. return response, err
  23. }
  24. if err := cli.NewVersionError("1.41", "specify container image platform"); platform != nil && err != nil {
  25. return response, err
  26. }
  27. if err := cli.NewVersionError("1.44", "specify health-check start interval"); config != nil && config.Healthcheck != nil && config.Healthcheck.StartInterval != 0 && err != nil {
  28. return response, err
  29. }
  30. if hostConfig != nil {
  31. if versions.LessThan(cli.ClientVersion(), "1.25") {
  32. // When using API 1.24 and under, the client is responsible for removing the container
  33. hostConfig.AutoRemove = false
  34. }
  35. if versions.GreaterThanOrEqualTo(cli.ClientVersion(), "1.42") || versions.LessThan(cli.ClientVersion(), "1.40") {
  36. // KernelMemory was added in API 1.40, and deprecated in API 1.42
  37. hostConfig.KernelMemory = 0
  38. }
  39. if platform != nil && platform.OS == "linux" && versions.LessThan(cli.ClientVersion(), "1.42") {
  40. // When using API under 1.42, the Linux daemon doesn't respect the ConsoleSize
  41. hostConfig.ConsoleSize = [2]uint{0, 0}
  42. }
  43. }
  44. query := url.Values{}
  45. if p := formatPlatform(platform); p != "" {
  46. query.Set("platform", p)
  47. }
  48. if containerName != "" {
  49. query.Set("name", containerName)
  50. }
  51. body := configWrapper{
  52. Config: config,
  53. HostConfig: hostConfig,
  54. NetworkingConfig: networkingConfig,
  55. }
  56. serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
  57. defer ensureReaderClosed(serverResp)
  58. if err != nil {
  59. return response, err
  60. }
  61. err = json.NewDecoder(serverResp.body).Decode(&response)
  62. return response, err
  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 *ocispec.Platform) string {
  70. if platform == nil {
  71. return ""
  72. }
  73. return path.Join(platform.OS, platform.Architecture, platform.Variant)
  74. }