create.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // +build linux,cgo
  2. package native
  3. import (
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "github.com/docker/docker/daemon/execdriver"
  9. "github.com/docker/docker/daemon/execdriver/native/template"
  10. "github.com/docker/libcontainer"
  11. "github.com/docker/libcontainer/apparmor"
  12. "github.com/docker/libcontainer/devices"
  13. "github.com/docker/libcontainer/mount"
  14. "github.com/docker/libcontainer/security/capabilities"
  15. )
  16. // createContainer populates and configures the container type with the
  17. // data provided by the execdriver.Command
  18. func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Config, error) {
  19. container := template.New()
  20. container.Hostname = getEnv("HOSTNAME", c.ProcessConfig.Env)
  21. container.Tty = c.ProcessConfig.Tty
  22. container.User = c.ProcessConfig.User
  23. container.WorkingDir = c.WorkingDir
  24. container.Env = c.ProcessConfig.Env
  25. container.Cgroups.Name = c.ID
  26. container.Cgroups.AllowedDevices = c.AllowedDevices
  27. container.MountConfig.DeviceNodes = c.AutoCreatedDevices
  28. // check to see if we are running in ramdisk to disable pivot root
  29. container.MountConfig.NoPivotRoot = os.Getenv("DOCKER_RAMDISK") != ""
  30. container.RestrictSys = true
  31. if err := d.createNetwork(container, c); err != nil {
  32. return nil, err
  33. }
  34. if c.ProcessConfig.Privileged {
  35. if err := d.setPrivileged(container); err != nil {
  36. return nil, err
  37. }
  38. } else {
  39. if err := d.setCapabilities(container, c); err != nil {
  40. return nil, err
  41. }
  42. }
  43. if c.AppArmorProfile != "" {
  44. container.AppArmorProfile = c.AppArmorProfile
  45. }
  46. if err := d.setupCgroups(container, c); err != nil {
  47. return nil, err
  48. }
  49. if err := d.setupMounts(container, c); err != nil {
  50. return nil, err
  51. }
  52. if err := d.setupLabels(container, c); err != nil {
  53. return nil, err
  54. }
  55. cmds := make(map[string]*exec.Cmd)
  56. d.Lock()
  57. for k, v := range d.activeContainers {
  58. cmds[k] = v.cmd
  59. }
  60. d.Unlock()
  61. return container, nil
  62. }
  63. func (d *driver) createNetwork(container *libcontainer.Config, c *execdriver.Command) error {
  64. if c.Network.HostNetworking {
  65. container.Namespaces["NEWNET"] = false
  66. return nil
  67. }
  68. container.Networks = []*libcontainer.Network{
  69. {
  70. Mtu: c.Network.Mtu,
  71. Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0),
  72. Gateway: "localhost",
  73. Type: "loopback",
  74. },
  75. }
  76. if c.Network.Interface != nil {
  77. vethNetwork := libcontainer.Network{
  78. Mtu: c.Network.Mtu,
  79. Address: fmt.Sprintf("%s/%d", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
  80. Gateway: c.Network.Interface.Gateway,
  81. Type: "veth",
  82. Bridge: c.Network.Interface.Bridge,
  83. VethPrefix: "veth",
  84. }
  85. container.Networks = append(container.Networks, &vethNetwork)
  86. }
  87. if c.Network.ContainerID != "" {
  88. d.Lock()
  89. active := d.activeContainers[c.Network.ContainerID]
  90. d.Unlock()
  91. if active == nil || active.cmd.Process == nil {
  92. return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
  93. }
  94. cmd := active.cmd
  95. nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
  96. container.Networks = append(container.Networks, &libcontainer.Network{
  97. Type: "netns",
  98. NsPath: nspath,
  99. })
  100. }
  101. return nil
  102. }
  103. func (d *driver) setPrivileged(container *libcontainer.Config) (err error) {
  104. container.Capabilities = capabilities.GetAllCapabilities()
  105. container.Cgroups.AllowAllDevices = true
  106. hostDeviceNodes, err := devices.GetHostDeviceNodes()
  107. if err != nil {
  108. return err
  109. }
  110. container.MountConfig.DeviceNodes = hostDeviceNodes
  111. container.RestrictSys = false
  112. if apparmor.IsEnabled() {
  113. container.AppArmorProfile = "unconfined"
  114. }
  115. return nil
  116. }
  117. func (d *driver) setCapabilities(container *libcontainer.Config, c *execdriver.Command) (err error) {
  118. container.Capabilities, err = execdriver.TweakCapabilities(container.Capabilities, c.CapAdd, c.CapDrop)
  119. return err
  120. }
  121. func (d *driver) setupCgroups(container *libcontainer.Config, c *execdriver.Command) error {
  122. if c.Resources != nil {
  123. container.Cgroups.CpuShares = c.Resources.CpuShares
  124. container.Cgroups.Memory = c.Resources.Memory
  125. container.Cgroups.MemoryReservation = c.Resources.Memory
  126. container.Cgroups.MemorySwap = c.Resources.MemorySwap
  127. container.Cgroups.CpusetCpus = c.Resources.Cpuset
  128. }
  129. return nil
  130. }
  131. func (d *driver) setupMounts(container *libcontainer.Config, c *execdriver.Command) error {
  132. for _, m := range c.Mounts {
  133. container.MountConfig.Mounts = append(container.MountConfig.Mounts, &mount.Mount{
  134. Type: "bind",
  135. Source: m.Source,
  136. Destination: m.Destination,
  137. Writable: m.Writable,
  138. Private: m.Private,
  139. Slave: m.Slave,
  140. })
  141. }
  142. return nil
  143. }
  144. func (d *driver) setupLabels(container *libcontainer.Config, c *execdriver.Command) error {
  145. container.ProcessLabel = c.ProcessLabel
  146. container.MountConfig.MountLabel = c.MountLabel
  147. return nil
  148. }