create.go 4.2 KB

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