builder.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/utils"
  5. "os"
  6. "path"
  7. "time"
  8. )
  9. var defaultDns = []string{"8.8.8.8", "8.8.4.4"}
  10. type Builder struct {
  11. runtime *Runtime
  12. repositories *TagStore
  13. graph *Graph
  14. config *Config
  15. image *Image
  16. }
  17. func NewBuilder(runtime *Runtime) *Builder {
  18. return &Builder{
  19. runtime: runtime,
  20. graph: runtime.graph,
  21. repositories: runtime.repositories,
  22. }
  23. }
  24. func (builder *Builder) Create(config *Config) (*Container, error) {
  25. // Lookup image
  26. img, err := builder.repositories.LookupImage(config.Image)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if img.Config != nil {
  31. MergeConfig(config, img.Config)
  32. }
  33. if len(config.Entrypoint) != 0 && config.Cmd == nil {
  34. config.Cmd = []string{}
  35. } else if config.Cmd == nil || len(config.Cmd) == 0 {
  36. return nil, fmt.Errorf("No command specified")
  37. }
  38. // Generate id
  39. id := GenerateID()
  40. // Generate default hostname
  41. // FIXME: the lxc template no longer needs to set a default hostname
  42. if config.Hostname == "" {
  43. config.Hostname = id[:12]
  44. }
  45. var args []string
  46. var entrypoint string
  47. if len(config.Entrypoint) != 0 {
  48. entrypoint = config.Entrypoint[0]
  49. args = append(config.Entrypoint[1:], config.Cmd...)
  50. } else {
  51. entrypoint = config.Cmd[0]
  52. args = config.Cmd[1:]
  53. }
  54. container := &Container{
  55. // FIXME: we should generate the ID here instead of receiving it as an argument
  56. ID: id,
  57. Created: time.Now(),
  58. Path: entrypoint,
  59. Args: args, //FIXME: de-duplicate from config
  60. Config: config,
  61. Image: img.ID, // Always use the resolved image id
  62. NetworkSettings: &NetworkSettings{},
  63. // FIXME: do we need to store this in the container?
  64. SysInitPath: sysInitPath,
  65. }
  66. container.root = builder.runtime.containerRoot(container.ID)
  67. // Step 1: create the container directory.
  68. // This doubles as a barrier to avoid race conditions.
  69. if err := os.Mkdir(container.root, 0700); err != nil {
  70. return nil, err
  71. }
  72. resolvConf, err := utils.GetResolvConf()
  73. if err != nil {
  74. return nil, err
  75. }
  76. if len(config.Dns) == 0 && len(builder.runtime.Dns) == 0 && utils.CheckLocalDns(resolvConf) {
  77. //"WARNING: Docker detected local DNS server on resolv.conf. Using default external servers: %v", defaultDns
  78. builder.runtime.Dns = defaultDns
  79. }
  80. // If custom dns exists, then create a resolv.conf for the container
  81. if len(config.Dns) > 0 || len(builder.runtime.Dns) > 0 {
  82. var dns []string
  83. if len(config.Dns) > 0 {
  84. dns = config.Dns
  85. } else {
  86. dns = builder.runtime.Dns
  87. }
  88. container.ResolvConfPath = path.Join(container.root, "resolv.conf")
  89. f, err := os.Create(container.ResolvConfPath)
  90. if err != nil {
  91. return nil, err
  92. }
  93. defer f.Close()
  94. for _, dns := range dns {
  95. if _, err := f.Write([]byte("nameserver " + dns + "\n")); err != nil {
  96. return nil, err
  97. }
  98. }
  99. } else {
  100. container.ResolvConfPath = "/etc/resolv.conf"
  101. }
  102. // Step 2: save the container json
  103. if err := container.ToDisk(); err != nil {
  104. return nil, err
  105. }
  106. // Step 3: register the container
  107. if err := builder.runtime.Register(container); err != nil {
  108. return nil, err
  109. }
  110. return container, nil
  111. }
  112. // Commit creates a new filesystem image from the current state of a container.
  113. // The image can optionally be tagged into a repository
  114. func (builder *Builder) Commit(container *Container, repository, tag, comment, author string, config *Config) (*Image, error) {
  115. // FIXME: freeze the container before copying it to avoid data corruption?
  116. // FIXME: this shouldn't be in commands.
  117. if err := container.EnsureMounted(); err != nil {
  118. return nil, err
  119. }
  120. rwTar, err := container.ExportRw()
  121. if err != nil {
  122. return nil, err
  123. }
  124. // Create a new image from the container's base layers + a new layer from container changes
  125. img, err := builder.graph.Create(rwTar, container, comment, author, config)
  126. if err != nil {
  127. return nil, err
  128. }
  129. // Register the image if needed
  130. if repository != "" {
  131. if err := builder.repositories.Set(repository, tag, img.ID, true); err != nil {
  132. return img, err
  133. }
  134. }
  135. return img, nil
  136. }