builder.go 3.1 KB

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