create.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. container.RootFs = c.Rootfs
  29. // check to see if we are running in ramdisk to disable pivot root
  30. container.MountConfig.NoPivotRoot = os.Getenv("DOCKER_RAMDISK") != ""
  31. container.RestrictSys = true
  32. if err := d.createIpc(container, c); err != nil {
  33. return nil, err
  34. }
  35. if err := d.createNetwork(container, c); err != nil {
  36. return nil, err
  37. }
  38. if c.ProcessConfig.Privileged {
  39. if err := d.setPrivileged(container); err != nil {
  40. return nil, err
  41. }
  42. } else {
  43. if err := d.setCapabilities(container, c); err != nil {
  44. return nil, err
  45. }
  46. }
  47. if c.AppArmorProfile != "" {
  48. container.AppArmorProfile = c.AppArmorProfile
  49. }
  50. if err := d.setupCgroups(container, c); err != nil {
  51. return nil, err
  52. }
  53. if err := d.setupMounts(container, c); err != nil {
  54. return nil, err
  55. }
  56. if err := d.setupLabels(container, c); err != nil {
  57. return nil, err
  58. }
  59. cmds := make(map[string]*exec.Cmd)
  60. d.Lock()
  61. for k, v := range d.activeContainers {
  62. cmds[k] = v.cmd
  63. }
  64. d.Unlock()
  65. return container, nil
  66. }
  67. func (d *driver) createNetwork(container *libcontainer.Config, c *execdriver.Command) error {
  68. if c.Network.HostNetworking {
  69. container.Namespaces["NEWNET"] = false
  70. return nil
  71. }
  72. container.Networks = []*libcontainer.Network{
  73. {
  74. Mtu: c.Network.Mtu,
  75. Address: fmt.Sprintf("%s/%d", "127.0.0.1", 0),
  76. Gateway: "localhost",
  77. Type: "loopback",
  78. },
  79. }
  80. if c.Network.Interface != nil {
  81. vethNetwork := libcontainer.Network{
  82. Mtu: c.Network.Mtu,
  83. Address: fmt.Sprintf("%s/%d", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
  84. MacAddress: c.Network.Interface.MacAddress,
  85. Gateway: c.Network.Interface.Gateway,
  86. Type: "veth",
  87. Bridge: c.Network.Interface.Bridge,
  88. VethPrefix: "veth",
  89. }
  90. container.Networks = append(container.Networks, &vethNetwork)
  91. }
  92. if c.Network.ContainerID != "" {
  93. d.Lock()
  94. active := d.activeContainers[c.Network.ContainerID]
  95. d.Unlock()
  96. if active == nil || active.cmd.Process == nil {
  97. return fmt.Errorf("%s is not a valid running container to join", c.Network.ContainerID)
  98. }
  99. cmd := active.cmd
  100. nspath := filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "net")
  101. container.Networks = append(container.Networks, &libcontainer.Network{
  102. Type: "netns",
  103. NsPath: nspath,
  104. })
  105. }
  106. return nil
  107. }
  108. func (d *driver) createIpc(container *libcontainer.Config, c *execdriver.Command) error {
  109. if c.Ipc.HostIpc {
  110. container.Namespaces["NEWIPC"] = false
  111. return nil
  112. }
  113. if c.Ipc.ContainerID != "" {
  114. d.Lock()
  115. active := d.activeContainers[c.Ipc.ContainerID]
  116. d.Unlock()
  117. if active == nil || active.cmd.Process == nil {
  118. return fmt.Errorf("%s is not a valid running container to join", c.Ipc.ContainerID)
  119. }
  120. cmd := active.cmd
  121. container.IpcNsPath = filepath.Join("/proc", fmt.Sprint(cmd.Process.Pid), "ns", "ipc")
  122. }
  123. return nil
  124. }
  125. func (d *driver) setPrivileged(container *libcontainer.Config) (err error) {
  126. container.Capabilities = capabilities.GetAllCapabilities()
  127. container.Cgroups.AllowAllDevices = true
  128. hostDeviceNodes, err := devices.GetHostDeviceNodes()
  129. if err != nil {
  130. return err
  131. }
  132. container.MountConfig.DeviceNodes = hostDeviceNodes
  133. container.RestrictSys = false
  134. if apparmor.IsEnabled() {
  135. container.AppArmorProfile = "unconfined"
  136. }
  137. return nil
  138. }
  139. func (d *driver) setCapabilities(container *libcontainer.Config, c *execdriver.Command) (err error) {
  140. container.Capabilities, err = execdriver.TweakCapabilities(container.Capabilities, c.CapAdd, c.CapDrop)
  141. return err
  142. }
  143. func (d *driver) setupCgroups(container *libcontainer.Config, c *execdriver.Command) error {
  144. if c.Resources != nil {
  145. container.Cgroups.CpuShares = c.Resources.CpuShares
  146. container.Cgroups.Memory = c.Resources.Memory
  147. container.Cgroups.MemoryReservation = c.Resources.Memory
  148. container.Cgroups.MemorySwap = c.Resources.MemorySwap
  149. container.Cgroups.CpusetCpus = c.Resources.Cpuset
  150. }
  151. return nil
  152. }
  153. func (d *driver) setupMounts(container *libcontainer.Config, c *execdriver.Command) error {
  154. for _, m := range c.Mounts {
  155. container.MountConfig.Mounts = append(container.MountConfig.Mounts, &mount.Mount{
  156. Type: "bind",
  157. Source: m.Source,
  158. Destination: m.Destination,
  159. Writable: m.Writable,
  160. Private: m.Private,
  161. Slave: m.Slave,
  162. })
  163. }
  164. return nil
  165. }
  166. func (d *driver) setupLabels(container *libcontainer.Config, c *execdriver.Command) error {
  167. container.ProcessLabel = c.ProcessLabel
  168. container.MountConfig.MountLabel = c.MountLabel
  169. return nil
  170. }