container_create.go 2.6 KB

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