container_create.go 2.8 KB

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