create.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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"
  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 = graph.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. if err = cli.stream("POST", "/images/create?"+v.Encode(), nil, out, map[string][]string{"X-Registry-Auth": registryAuthHeader}); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. type cidFile struct {
  49. path string
  50. file *os.File
  51. written bool
  52. }
  53. func newCIDFile(path string) (*cidFile, error) {
  54. if _, err := os.Stat(path); err == nil {
  55. return nil, fmt.Errorf("Container ID file found, make sure the other container isn't running or delete %s", path)
  56. }
  57. f, err := os.Create(path)
  58. if err != nil {
  59. return nil, fmt.Errorf("Failed to create the container ID file: %s", err)
  60. }
  61. return &cidFile{path: path, file: f}, nil
  62. }
  63. func (cli *DockerCli) createContainer(config *runconfig.Config, hostConfig *runconfig.HostConfig, cidfile, name string) (*types.ContainerCreateResponse, error) {
  64. containerValues := url.Values{}
  65. if name != "" {
  66. containerValues.Set("name", name)
  67. }
  68. mergedConfig := runconfig.MergeConfigs(config, hostConfig)
  69. var containerIDFile *cidFile
  70. if cidfile != "" {
  71. var err error
  72. if containerIDFile, err = newCIDFile(cidfile); err != nil {
  73. return nil, err
  74. }
  75. defer containerIDFile.Close()
  76. }
  77. //create the container
  78. stream, statusCode, err := cli.call("POST", "/containers/create?"+containerValues.Encode(), mergedConfig, nil)
  79. //if image not found try to pull it
  80. if statusCode == 404 && strings.Contains(err.Error(), config.Image) {
  81. repo, tag := parsers.ParseRepositoryTag(config.Image)
  82. if tag == "" {
  83. tag = graph.DEFAULTTAG
  84. }
  85. fmt.Fprintf(cli.err, "Unable to find image '%s' locally\n", utils.ImageReference(repo, tag))
  86. // we don't want to write to stdout anything apart from container.ID
  87. if err = cli.pullImageCustomOut(config.Image, cli.err); err != nil {
  88. return nil, err
  89. }
  90. // Retry
  91. if stream, _, err = cli.call("POST", "/containers/create?"+containerValues.Encode(), mergedConfig, nil); err != nil {
  92. return nil, err
  93. }
  94. } else if err != nil {
  95. return nil, err
  96. }
  97. var response types.ContainerCreateResponse
  98. if err := json.NewDecoder(stream).Decode(&response); err != nil {
  99. return nil, err
  100. }
  101. for _, warning := range response.Warnings {
  102. fmt.Fprintf(cli.err, "WARNING: %s\n", warning)
  103. }
  104. if containerIDFile != nil {
  105. if err = containerIDFile.Write(response.ID); err != nil {
  106. return nil, err
  107. }
  108. }
  109. return &response, nil
  110. }
  111. // CmdCreate creates a new container from a given image.
  112. //
  113. // Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
  114. func (cli *DockerCli) CmdCreate(args ...string) error {
  115. cmd := cli.Subcmd("create", "IMAGE [COMMAND] [ARG...]", "Create a new container", true)
  116. // These are flags not stored in Config/HostConfig
  117. var (
  118. flName = cmd.String([]string{"-name"}, "", "Assign a name to the container")
  119. )
  120. config, hostConfig, cmd, err := runconfig.Parse(cmd, args)
  121. if err != nil {
  122. cmd.ReportError(err.Error(), true)
  123. }
  124. if config.Image == "" {
  125. cmd.Usage()
  126. return nil
  127. }
  128. response, err := cli.createContainer(config, hostConfig, hostConfig.ContainerIDFile, *flName)
  129. if err != nil {
  130. return err
  131. }
  132. fmt.Fprintf(cli.out, "%s\n", response.ID)
  133. return nil
  134. }