create.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package daemon
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/docker/docker/graph"
  8. "github.com/docker/docker/image"
  9. "github.com/docker/docker/pkg/parsers"
  10. "github.com/docker/docker/pkg/stringid"
  11. "github.com/docker/docker/runconfig"
  12. "github.com/docker/libcontainer/label"
  13. )
  14. func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig) (string, []string, error) {
  15. if config == nil {
  16. return "", nil, fmt.Errorf("Config cannot be empty in order to create a container")
  17. }
  18. warnings, err := daemon.verifyHostConfig(hostConfig)
  19. if err != nil {
  20. return "", warnings, err
  21. }
  22. // The check for a valid workdir path is made on the server rather than in the
  23. // client. This is because we don't know the type of path (Linux or Windows)
  24. // to validate on the client.
  25. if config.WorkingDir != "" && !filepath.IsAbs(config.WorkingDir) {
  26. return "", warnings, fmt.Errorf("The working directory '%s' is invalid. It needs to be an absolute path.", config.WorkingDir)
  27. }
  28. container, buildWarnings, err := daemon.Create(config, hostConfig, name)
  29. if err != nil {
  30. if daemon.Graph().IsNotExist(err, config.Image) {
  31. _, tag := parsers.ParseRepositoryTag(config.Image)
  32. if tag == "" {
  33. tag = graph.DEFAULTTAG
  34. }
  35. return "", warnings, fmt.Errorf("No such image: %s (tag: %s)", config.Image, tag)
  36. }
  37. return "", warnings, err
  38. }
  39. warnings = append(warnings, buildWarnings...)
  40. return container.ID, warnings, nil
  41. }
  42. // Create creates a new container from the given configuration with a given name.
  43. func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.HostConfig, name string) (*Container, []string, error) {
  44. var (
  45. container *Container
  46. warnings []string
  47. img *image.Image
  48. imgID string
  49. err error
  50. )
  51. if config.Image != "" {
  52. img, err = daemon.repositories.LookupImage(config.Image)
  53. if err != nil {
  54. return nil, nil, err
  55. }
  56. if err = img.CheckDepth(); err != nil {
  57. return nil, nil, err
  58. }
  59. imgID = img.ID
  60. }
  61. if err := daemon.mergeAndVerifyConfig(config, img); err != nil {
  62. return nil, nil, err
  63. }
  64. if !config.NetworkDisabled && daemon.SystemConfig().IPv4ForwardingDisabled {
  65. warnings = append(warnings, "IPv4 forwarding is disabled.")
  66. }
  67. if hostConfig == nil {
  68. hostConfig = &runconfig.HostConfig{}
  69. }
  70. if hostConfig.SecurityOpt == nil {
  71. hostConfig.SecurityOpt, err = daemon.GenerateSecurityOpt(hostConfig.IpcMode, hostConfig.PidMode)
  72. if err != nil {
  73. return nil, nil, err
  74. }
  75. }
  76. if container, err = daemon.newContainer(name, config, imgID); err != nil {
  77. return nil, nil, err
  78. }
  79. if err := daemon.Register(container); err != nil {
  80. return nil, nil, err
  81. }
  82. if err := daemon.createRootfs(container); err != nil {
  83. return nil, nil, err
  84. }
  85. if err := daemon.setHostConfig(container, hostConfig); err != nil {
  86. return nil, nil, err
  87. }
  88. if err := container.Mount(); err != nil {
  89. return nil, nil, err
  90. }
  91. defer container.Unmount()
  92. for spec := range config.Volumes {
  93. var (
  94. name, destination string
  95. parts = strings.Split(spec, ":")
  96. )
  97. switch len(parts) {
  98. case 2:
  99. name, destination = parts[0], filepath.Clean(parts[1])
  100. default:
  101. name = stringid.GenerateRandomID()
  102. destination = filepath.Clean(parts[0])
  103. }
  104. // Skip volumes for which we already have something mounted on that
  105. // destination because of a --volume-from.
  106. if container.isDestinationMounted(destination) {
  107. continue
  108. }
  109. path, err := container.GetResourcePath(destination)
  110. if err != nil {
  111. return nil, nil, err
  112. }
  113. stat, err := os.Stat(path)
  114. if err == nil && !stat.IsDir() {
  115. return nil, nil, fmt.Errorf("cannot mount volume over existing file, file exists %s", path)
  116. }
  117. v, err := createVolume(name, config.VolumeDriver)
  118. if err != nil {
  119. return nil, nil, err
  120. }
  121. if err := label.Relabel(v.Path(), container.MountLabel, "z"); err != nil {
  122. return nil, nil, err
  123. }
  124. if err := container.copyImagePathContent(v, destination); err != nil {
  125. return nil, nil, err
  126. }
  127. container.addMountPointWithVolume(destination, v, true)
  128. }
  129. if err := container.ToDisk(); err != nil {
  130. return nil, nil, err
  131. }
  132. container.LogEvent("create")
  133. return container, warnings, nil
  134. }
  135. func (daemon *Daemon) GenerateSecurityOpt(ipcMode runconfig.IpcMode, pidMode runconfig.PidMode) ([]string, error) {
  136. if ipcMode.IsHost() || pidMode.IsHost() {
  137. return label.DisableSecOpt(), nil
  138. }
  139. if ipcContainer := ipcMode.Container(); ipcContainer != "" {
  140. c, err := daemon.Get(ipcContainer)
  141. if err != nil {
  142. return nil, err
  143. }
  144. return label.DupSecOpt(c.ProcessLabel), nil
  145. }
  146. return nil, nil
  147. }