create.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package client
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/url"
  8. "os"
  9. "strings"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/graph/tags"
  12. "github.com/docker/docker/pkg/parsers"
  13. "github.com/docker/docker/registry"
  14. "github.com/docker/docker/runconfig"
  15. "github.com/docker/docker/utils"
  16. )
  17. func (cli *DockerCli) pullImage(image string) error {
  18. return cli.pullImageCustomOut(image, cli.out)
  19. }
  20. func (cli *DockerCli) pullImageCustomOut(image string, out io.Writer) error {
  21. v := url.Values{}
  22. repos, tag := parsers.ParseRepositoryTag(image)
  23. // pull only the image tagged 'latest' if no tag was specified
  24. if tag == "" {
  25. tag = tags.DEFAULTTAG
  26. }
  27. v.Set("fromImage", repos)
  28. v.Set("tag", tag)
  29. // Resolve the Repository name from fqn to RepositoryInfo
  30. repoInfo, err := registry.ParseRepositoryInfo(repos)
  31. if err != nil {
  32. return err
  33. }
  34. // Resolve the Auth config relevant for this server
  35. authConfig := registry.ResolveAuthConfig(cli.configFile, repoInfo.Index)
  36. buf, err := json.Marshal(authConfig)
  37. if err != nil {
  38. return err
  39. }
  40. registryAuthHeader := []string{
  41. base64.URLEncoding.EncodeToString(buf),
  42. }
  43. sopts := &streamOpts{
  44. rawTerminal: true,
  45. out: out,
  46. headers: map[string][]string{"X-Registry-Auth": registryAuthHeader},
  47. }
  48. if err := cli.stream("POST", "/images/create?"+v.Encode(), sopts); err != nil {
  49. return err
  50. }
  51. return nil
  52. }
  53. type cidFile struct {
  54. path string
  55. file *os.File
  56. written bool
  57. }
  58. func newCIDFile(path string) (*cidFile, error) {
  59. if _, err := os.Stat(path); err == nil {
  60. return nil, fmt.Errorf("Container ID file found, make sure the other container isn't running or delete %s", path)
  61. }
  62. f, err := os.Create(path)
  63. if err != nil {
  64. return nil, fmt.Errorf("Failed to create the container ID file: %s", err)
  65. }
  66. return &cidFile{path: path, file: f}, nil
  67. }
  68. func (cli *DockerCli) createContainer(config *runconfig.Config, hostConfig *runconfig.HostConfig, cidfile, name string) (*types.ContainerCreateResponse, error) {
  69. containerValues := url.Values{}
  70. if name != "" {
  71. containerValues.Set("name", name)
  72. }
  73. mergedConfig := runconfig.MergeConfigs(config, hostConfig)
  74. var containerIDFile *cidFile
  75. if cidfile != "" {
  76. var err error
  77. if containerIDFile, err = newCIDFile(cidfile); err != nil {
  78. return nil, err
  79. }
  80. defer containerIDFile.Close()
  81. }
  82. //create the container
  83. stream, statusCode, err := cli.call("POST", "/containers/create?"+containerValues.Encode(), mergedConfig, nil)
  84. //if image not found try to pull it
  85. if statusCode == 404 && strings.Contains(err.Error(), config.Image) {
  86. repo, tag := parsers.ParseRepositoryTag(config.Image)
  87. if tag == "" {
  88. tag = tags.DEFAULTTAG
  89. }
  90. fmt.Fprintf(cli.err, "Unable to find image '%s' locally\n", utils.ImageReference(repo, tag))
  91. // we don't want to write to stdout anything apart from container.ID
  92. if err = cli.pullImageCustomOut(config.Image, cli.err); err != nil {
  93. return nil, err
  94. }
  95. // Retry
  96. if stream, _, err = cli.call("POST", "/containers/create?"+containerValues.Encode(), mergedConfig, nil); err != nil {
  97. return nil, err
  98. }
  99. } else if err != nil {
  100. return nil, err
  101. }
  102. var response types.ContainerCreateResponse
  103. if err := json.NewDecoder(stream).Decode(&response); err != nil {
  104. return nil, err
  105. }
  106. for _, warning := range response.Warnings {
  107. fmt.Fprintf(cli.err, "WARNING: %s\n", warning)
  108. }
  109. if containerIDFile != nil {
  110. if err = containerIDFile.Write(response.ID); err != nil {
  111. return nil, err
  112. }
  113. }
  114. return &response, nil
  115. }
  116. // CmdCreate creates a new container from a given image.
  117. //
  118. // Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
  119. func (cli *DockerCli) CmdCreate(args ...string) error {
  120. cmd := cli.Subcmd("create", "IMAGE [COMMAND] [ARG...]", "Create a new container", true)
  121. // These are flags not stored in Config/HostConfig
  122. var (
  123. flName = cmd.String([]string{"-name"}, "", "Assign a name to the container")
  124. )
  125. config, hostConfig, cmd, err := runconfig.Parse(cmd, args)
  126. if err != nil {
  127. cmd.ReportError(err.Error(), true)
  128. os.Exit(1)
  129. }
  130. if config.Image == "" {
  131. cmd.Usage()
  132. return nil
  133. }
  134. response, err := cli.createContainer(config, hostConfig, hostConfig.ContainerIDFile, *flName)
  135. if err != nil {
  136. return err
  137. }
  138. fmt.Fprintf(cli.out, "%s\n", response.ID)
  139. return nil
  140. }