container_unix.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. // +build linux freebsd
  2. package daemon
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "syscall"
  13. "time"
  14. "github.com/Sirupsen/logrus"
  15. "github.com/docker/docker/daemon/execdriver"
  16. "github.com/docker/docker/daemon/links"
  17. "github.com/docker/docker/daemon/network"
  18. "github.com/docker/docker/pkg/directory"
  19. "github.com/docker/docker/pkg/nat"
  20. "github.com/docker/docker/pkg/stringid"
  21. "github.com/docker/docker/pkg/system"
  22. "github.com/docker/docker/pkg/ulimit"
  23. "github.com/docker/docker/runconfig"
  24. "github.com/docker/docker/utils"
  25. "github.com/docker/docker/volume"
  26. "github.com/docker/libnetwork"
  27. "github.com/docker/libnetwork/netlabel"
  28. "github.com/docker/libnetwork/options"
  29. "github.com/docker/libnetwork/types"
  30. "github.com/opencontainers/runc/libcontainer/configs"
  31. "github.com/opencontainers/runc/libcontainer/devices"
  32. "github.com/opencontainers/runc/libcontainer/label"
  33. )
  34. const DefaultPathEnv = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  35. type Container struct {
  36. CommonContainer
  37. // Fields below here are platform specific.
  38. activeLinks map[string]*links.Link
  39. AppArmorProfile string
  40. HostnamePath string
  41. HostsPath string
  42. MountPoints map[string]*mountPoint
  43. ResolvConfPath string
  44. UpdateDns bool
  45. Volumes map[string]string // Deprecated since 1.7, kept for backwards compatibility
  46. VolumesRW map[string]bool // Deprecated since 1.7, kept for backwards compatibility
  47. }
  48. func killProcessDirectly(container *Container) error {
  49. if _, err := container.WaitStop(10 * time.Second); err != nil {
  50. // Ensure that we don't kill ourselves
  51. if pid := container.GetPid(); pid != 0 {
  52. logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
  53. if err := syscall.Kill(pid, 9); err != nil {
  54. if err != syscall.ESRCH {
  55. return err
  56. }
  57. logrus.Debugf("Cannot kill process (pid=%d) with signal 9: no such process.", pid)
  58. }
  59. }
  60. }
  61. return nil
  62. }
  63. func (container *Container) setupLinkedContainers() ([]string, error) {
  64. var (
  65. env []string
  66. daemon = container.daemon
  67. )
  68. children, err := daemon.Children(container.Name)
  69. if err != nil {
  70. return nil, err
  71. }
  72. if len(children) > 0 {
  73. for linkAlias, child := range children {
  74. if !child.IsRunning() {
  75. return nil, fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
  76. }
  77. link := links.NewLink(
  78. container.NetworkSettings.IPAddress,
  79. child.NetworkSettings.IPAddress,
  80. linkAlias,
  81. child.Config.Env,
  82. child.Config.ExposedPorts,
  83. )
  84. for _, envVar := range link.ToEnv() {
  85. env = append(env, envVar)
  86. }
  87. }
  88. }
  89. return env, nil
  90. }
  91. func (container *Container) createDaemonEnvironment(linkedEnv []string) []string {
  92. // if a domain name was specified, append it to the hostname (see #7851)
  93. fullHostname := container.Config.Hostname
  94. if container.Config.Domainname != "" {
  95. fullHostname = fmt.Sprintf("%s.%s", fullHostname, container.Config.Domainname)
  96. }
  97. // Setup environment
  98. env := []string{
  99. "PATH=" + DefaultPathEnv,
  100. "HOSTNAME=" + fullHostname,
  101. // Note: we don't set HOME here because it'll get autoset intelligently
  102. // based on the value of USER inside dockerinit, but only if it isn't
  103. // set already (ie, that can be overridden by setting HOME via -e or ENV
  104. // in a Dockerfile).
  105. }
  106. if container.Config.Tty {
  107. env = append(env, "TERM=xterm")
  108. }
  109. env = append(env, linkedEnv...)
  110. // because the env on the container can override certain default values
  111. // we need to replace the 'env' keys where they match and append anything
  112. // else.
  113. env = utils.ReplaceOrAppendEnvValues(env, container.Config.Env)
  114. return env
  115. }
  116. func getDevicesFromPath(deviceMapping runconfig.DeviceMapping) (devs []*configs.Device, err error) {
  117. device, err := devices.DeviceFromPath(deviceMapping.PathOnHost, deviceMapping.CgroupPermissions)
  118. // if there was no error, return the device
  119. if err == nil {
  120. device.Path = deviceMapping.PathInContainer
  121. return append(devs, device), nil
  122. }
  123. // if the device is not a device node
  124. // try to see if it's a directory holding many devices
  125. if err == devices.ErrNotADevice {
  126. // check if it is a directory
  127. if src, e := os.Stat(deviceMapping.PathOnHost); e == nil && src.IsDir() {
  128. // mount the internal devices recursively
  129. filepath.Walk(deviceMapping.PathOnHost, func(dpath string, f os.FileInfo, e error) error {
  130. childDevice, e := devices.DeviceFromPath(dpath, deviceMapping.CgroupPermissions)
  131. if e != nil {
  132. // ignore the device
  133. return nil
  134. }
  135. // add the device to userSpecified devices
  136. childDevice.Path = strings.Replace(dpath, deviceMapping.PathOnHost, deviceMapping.PathInContainer, 1)
  137. devs = append(devs, childDevice)
  138. return nil
  139. })
  140. }
  141. }
  142. if len(devs) > 0 {
  143. return devs, nil
  144. }
  145. return devs, fmt.Errorf("error gathering device information while adding custom device %q: %s", deviceMapping.PathOnHost, err)
  146. }
  147. func populateCommand(c *Container, env []string) error {
  148. var en *execdriver.Network
  149. if !c.Config.NetworkDisabled {
  150. en = &execdriver.Network{
  151. NamespacePath: c.NetworkSettings.SandboxKey,
  152. }
  153. parts := strings.SplitN(string(c.hostConfig.NetworkMode), ":", 2)
  154. if parts[0] == "container" {
  155. nc, err := c.getNetworkedContainer()
  156. if err != nil {
  157. return err
  158. }
  159. en.ContainerID = nc.ID
  160. }
  161. }
  162. ipc := &execdriver.Ipc{}
  163. if c.hostConfig.IpcMode.IsContainer() {
  164. ic, err := c.getIpcContainer()
  165. if err != nil {
  166. return err
  167. }
  168. ipc.ContainerID = ic.ID
  169. } else {
  170. ipc.HostIpc = c.hostConfig.IpcMode.IsHost()
  171. }
  172. pid := &execdriver.Pid{}
  173. pid.HostPid = c.hostConfig.PidMode.IsHost()
  174. uts := &execdriver.UTS{
  175. HostUTS: c.hostConfig.UTSMode.IsHost(),
  176. }
  177. // Build lists of devices allowed and created within the container.
  178. var userSpecifiedDevices []*configs.Device
  179. for _, deviceMapping := range c.hostConfig.Devices {
  180. devs, err := getDevicesFromPath(deviceMapping)
  181. if err != nil {
  182. return err
  183. }
  184. userSpecifiedDevices = append(userSpecifiedDevices, devs...)
  185. }
  186. allowedDevices := mergeDevices(configs.DefaultAllowedDevices, userSpecifiedDevices)
  187. autoCreatedDevices := mergeDevices(configs.DefaultAutoCreatedDevices, userSpecifiedDevices)
  188. // TODO: this can be removed after lxc-conf is fully deprecated
  189. lxcConfig, err := mergeLxcConfIntoOptions(c.hostConfig)
  190. if err != nil {
  191. return err
  192. }
  193. var rlimits []*ulimit.Rlimit
  194. ulimits := c.hostConfig.Ulimits
  195. // Merge ulimits with daemon defaults
  196. ulIdx := make(map[string]*ulimit.Ulimit)
  197. for _, ul := range ulimits {
  198. ulIdx[ul.Name] = ul
  199. }
  200. for name, ul := range c.daemon.config.Ulimits {
  201. if _, exists := ulIdx[name]; !exists {
  202. ulimits = append(ulimits, ul)
  203. }
  204. }
  205. for _, limit := range ulimits {
  206. rl, err := limit.GetRlimit()
  207. if err != nil {
  208. return err
  209. }
  210. rlimits = append(rlimits, rl)
  211. }
  212. resources := &execdriver.Resources{
  213. Memory: c.hostConfig.Memory,
  214. MemorySwap: c.hostConfig.MemorySwap,
  215. CPUShares: c.hostConfig.CPUShares,
  216. CpusetCpus: c.hostConfig.CpusetCpus,
  217. CpusetMems: c.hostConfig.CpusetMems,
  218. CPUPeriod: c.hostConfig.CPUPeriod,
  219. CPUQuota: c.hostConfig.CPUQuota,
  220. BlkioWeight: c.hostConfig.BlkioWeight,
  221. Rlimits: rlimits,
  222. OomKillDisable: c.hostConfig.OomKillDisable,
  223. MemorySwappiness: -1,
  224. }
  225. if c.hostConfig.MemorySwappiness != nil {
  226. resources.MemorySwappiness = *c.hostConfig.MemorySwappiness
  227. }
  228. processConfig := execdriver.ProcessConfig{
  229. Privileged: c.hostConfig.Privileged,
  230. Entrypoint: c.Path,
  231. Arguments: c.Args,
  232. Tty: c.Config.Tty,
  233. User: c.Config.User,
  234. }
  235. processConfig.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
  236. processConfig.Env = env
  237. c.command = &execdriver.Command{
  238. ID: c.ID,
  239. Rootfs: c.RootfsPath(),
  240. ReadonlyRootfs: c.hostConfig.ReadonlyRootfs,
  241. InitPath: "/.dockerinit",
  242. WorkingDir: c.Config.WorkingDir,
  243. Network: en,
  244. Ipc: ipc,
  245. Pid: pid,
  246. UTS: uts,
  247. Resources: resources,
  248. AllowedDevices: allowedDevices,
  249. AutoCreatedDevices: autoCreatedDevices,
  250. CapAdd: c.hostConfig.CapAdd.Slice(),
  251. CapDrop: c.hostConfig.CapDrop.Slice(),
  252. GroupAdd: c.hostConfig.GroupAdd,
  253. ProcessConfig: processConfig,
  254. ProcessLabel: c.GetProcessLabel(),
  255. MountLabel: c.GetMountLabel(),
  256. LxcConfig: lxcConfig,
  257. AppArmorProfile: c.AppArmorProfile,
  258. CgroupParent: c.hostConfig.CgroupParent,
  259. }
  260. return nil
  261. }
  262. func mergeDevices(defaultDevices, userDevices []*configs.Device) []*configs.Device {
  263. if len(userDevices) == 0 {
  264. return defaultDevices
  265. }
  266. paths := map[string]*configs.Device{}
  267. for _, d := range userDevices {
  268. paths[d.Path] = d
  269. }
  270. var devs []*configs.Device
  271. for _, d := range defaultDevices {
  272. if _, defined := paths[d.Path]; !defined {
  273. devs = append(devs, d)
  274. }
  275. }
  276. return append(devs, userDevices...)
  277. }
  278. // GetSize, return real size, virtual size
  279. func (container *Container) GetSize() (int64, int64) {
  280. var (
  281. sizeRw, sizeRootfs int64
  282. err error
  283. driver = container.daemon.driver
  284. )
  285. if err := container.Mount(); err != nil {
  286. logrus.Errorf("Failed to compute size of container rootfs %s: %s", container.ID, err)
  287. return sizeRw, sizeRootfs
  288. }
  289. defer container.Unmount()
  290. initID := fmt.Sprintf("%s-init", container.ID)
  291. sizeRw, err = driver.DiffSize(container.ID, initID)
  292. if err != nil {
  293. logrus.Errorf("Driver %s couldn't return diff size of container %s: %s", driver, container.ID, err)
  294. // FIXME: GetSize should return an error. Not changing it now in case
  295. // there is a side-effect.
  296. sizeRw = -1
  297. }
  298. if _, err = os.Stat(container.basefs); err == nil {
  299. if sizeRootfs, err = directory.Size(container.basefs); err != nil {
  300. sizeRootfs = -1
  301. }
  302. }
  303. return sizeRw, sizeRootfs
  304. }
  305. // Attempt to set the network mounts given a provided destination and
  306. // the path to use for it; return true if the given destination was a
  307. // network mount file
  308. func (container *Container) trySetNetworkMount(destination string, path string) bool {
  309. if destination == "/etc/resolv.conf" {
  310. container.ResolvConfPath = path
  311. return true
  312. }
  313. if destination == "/etc/hostname" {
  314. container.HostnamePath = path
  315. return true
  316. }
  317. if destination == "/etc/hosts" {
  318. container.HostsPath = path
  319. return true
  320. }
  321. return false
  322. }
  323. func (container *Container) buildHostnameFile() error {
  324. hostnamePath, err := container.GetRootResourcePath("hostname")
  325. if err != nil {
  326. return err
  327. }
  328. container.HostnamePath = hostnamePath
  329. if container.Config.Domainname != "" {
  330. return ioutil.WriteFile(container.HostnamePath, []byte(fmt.Sprintf("%s.%s\n", container.Config.Hostname, container.Config.Domainname)), 0644)
  331. }
  332. return ioutil.WriteFile(container.HostnamePath, []byte(container.Config.Hostname+"\n"), 0644)
  333. }
  334. func (container *Container) buildJoinOptions() ([]libnetwork.EndpointOption, error) {
  335. var (
  336. joinOptions []libnetwork.EndpointOption
  337. err error
  338. dns []string
  339. dnsSearch []string
  340. )
  341. joinOptions = append(joinOptions, libnetwork.JoinOptionHostname(container.Config.Hostname),
  342. libnetwork.JoinOptionDomainname(container.Config.Domainname))
  343. if container.hostConfig.NetworkMode.IsHost() {
  344. joinOptions = append(joinOptions, libnetwork.JoinOptionUseDefaultSandbox())
  345. }
  346. container.HostsPath, err = container.GetRootResourcePath("hosts")
  347. if err != nil {
  348. return nil, err
  349. }
  350. joinOptions = append(joinOptions, libnetwork.JoinOptionHostsPath(container.HostsPath))
  351. container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
  352. if err != nil {
  353. return nil, err
  354. }
  355. joinOptions = append(joinOptions, libnetwork.JoinOptionResolvConfPath(container.ResolvConfPath))
  356. if len(container.hostConfig.DNS) > 0 {
  357. dns = container.hostConfig.DNS
  358. } else if len(container.daemon.config.Dns) > 0 {
  359. dns = container.daemon.config.Dns
  360. }
  361. for _, d := range dns {
  362. joinOptions = append(joinOptions, libnetwork.JoinOptionDNS(d))
  363. }
  364. if len(container.hostConfig.DNSSearch) > 0 {
  365. dnsSearch = container.hostConfig.DNSSearch
  366. } else if len(container.daemon.config.DnsSearch) > 0 {
  367. dnsSearch = container.daemon.config.DnsSearch
  368. }
  369. for _, ds := range dnsSearch {
  370. joinOptions = append(joinOptions, libnetwork.JoinOptionDNSSearch(ds))
  371. }
  372. if container.NetworkSettings.SecondaryIPAddresses != nil {
  373. name := container.Config.Hostname
  374. if container.Config.Domainname != "" {
  375. name = name + "." + container.Config.Domainname
  376. }
  377. for _, a := range container.NetworkSettings.SecondaryIPAddresses {
  378. joinOptions = append(joinOptions, libnetwork.JoinOptionExtraHost(name, a.Addr))
  379. }
  380. }
  381. var childEndpoints, parentEndpoints []string
  382. children, err := container.daemon.Children(container.Name)
  383. if err != nil {
  384. return nil, err
  385. }
  386. for linkAlias, child := range children {
  387. _, alias := path.Split(linkAlias)
  388. // allow access to the linked container via the alias, real name, and container hostname
  389. aliasList := alias + " " + child.Config.Hostname
  390. // only add the name if alias isn't equal to the name
  391. if alias != child.Name[1:] {
  392. aliasList = aliasList + " " + child.Name[1:]
  393. }
  394. joinOptions = append(joinOptions, libnetwork.JoinOptionExtraHost(aliasList, child.NetworkSettings.IPAddress))
  395. if child.NetworkSettings.EndpointID != "" {
  396. childEndpoints = append(childEndpoints, child.NetworkSettings.EndpointID)
  397. }
  398. }
  399. for _, extraHost := range container.hostConfig.ExtraHosts {
  400. // allow IPv6 addresses in extra hosts; only split on first ":"
  401. parts := strings.SplitN(extraHost, ":", 2)
  402. joinOptions = append(joinOptions, libnetwork.JoinOptionExtraHost(parts[0], parts[1]))
  403. }
  404. refs := container.daemon.ContainerGraph().RefPaths(container.ID)
  405. for _, ref := range refs {
  406. if ref.ParentID == "0" {
  407. continue
  408. }
  409. c, err := container.daemon.Get(ref.ParentID)
  410. if err != nil {
  411. logrus.Error(err)
  412. }
  413. if c != nil && !container.daemon.config.DisableBridge && container.hostConfig.NetworkMode.IsPrivate() {
  414. logrus.Debugf("Update /etc/hosts of %s for alias %s with ip %s", c.ID, ref.Name, container.NetworkSettings.IPAddress)
  415. joinOptions = append(joinOptions, libnetwork.JoinOptionParentUpdate(c.NetworkSettings.EndpointID, ref.Name, container.NetworkSettings.IPAddress))
  416. if c.NetworkSettings.EndpointID != "" {
  417. parentEndpoints = append(parentEndpoints, c.NetworkSettings.EndpointID)
  418. }
  419. }
  420. }
  421. linkOptions := options.Generic{
  422. netlabel.GenericData: options.Generic{
  423. "ParentEndpoints": parentEndpoints,
  424. "ChildEndpoints": childEndpoints,
  425. },
  426. }
  427. joinOptions = append(joinOptions, libnetwork.JoinOptionGeneric(linkOptions))
  428. return joinOptions, nil
  429. }
  430. func (container *Container) buildPortMapInfo(n libnetwork.Network, ep libnetwork.Endpoint, networkSettings *network.Settings) (*network.Settings, error) {
  431. if ep == nil {
  432. return nil, fmt.Errorf("invalid endpoint while building port map info")
  433. }
  434. if networkSettings == nil {
  435. return nil, fmt.Errorf("invalid networksettings while building port map info")
  436. }
  437. driverInfo, err := ep.DriverInfo()
  438. if err != nil {
  439. return nil, err
  440. }
  441. if driverInfo == nil {
  442. // It is not an error for epInfo to be nil
  443. return networkSettings, nil
  444. }
  445. if mac, ok := driverInfo[netlabel.MacAddress]; ok {
  446. networkSettings.MacAddress = mac.(net.HardwareAddr).String()
  447. }
  448. networkSettings.Ports = nat.PortMap{}
  449. if expData, ok := driverInfo[netlabel.ExposedPorts]; ok {
  450. if exposedPorts, ok := expData.([]types.TransportPort); ok {
  451. for _, tp := range exposedPorts {
  452. natPort, err := nat.NewPort(tp.Proto.String(), strconv.Itoa(int(tp.Port)))
  453. if err != nil {
  454. return nil, fmt.Errorf("Error parsing Port value(%v):%v", tp.Port, err)
  455. }
  456. networkSettings.Ports[natPort] = nil
  457. }
  458. }
  459. }
  460. mapData, ok := driverInfo[netlabel.PortMap]
  461. if !ok {
  462. return networkSettings, nil
  463. }
  464. if portMapping, ok := mapData.([]types.PortBinding); ok {
  465. for _, pp := range portMapping {
  466. natPort, err := nat.NewPort(pp.Proto.String(), strconv.Itoa(int(pp.Port)))
  467. if err != nil {
  468. return nil, err
  469. }
  470. natBndg := nat.PortBinding{HostIP: pp.HostIP.String(), HostPort: strconv.Itoa(int(pp.HostPort))}
  471. networkSettings.Ports[natPort] = append(networkSettings.Ports[natPort], natBndg)
  472. }
  473. }
  474. return networkSettings, nil
  475. }
  476. func (container *Container) buildEndpointInfo(n libnetwork.Network, ep libnetwork.Endpoint, networkSettings *network.Settings) (*network.Settings, error) {
  477. if ep == nil {
  478. return nil, fmt.Errorf("invalid endpoint while building port map info")
  479. }
  480. if networkSettings == nil {
  481. return nil, fmt.Errorf("invalid networksettings while building port map info")
  482. }
  483. epInfo := ep.Info()
  484. if epInfo == nil {
  485. // It is not an error to get an empty endpoint info
  486. return networkSettings, nil
  487. }
  488. ifaceList := epInfo.InterfaceList()
  489. if len(ifaceList) == 0 {
  490. return networkSettings, nil
  491. }
  492. iface := ifaceList[0]
  493. ones, _ := iface.Address().Mask.Size()
  494. networkSettings.IPAddress = iface.Address().IP.String()
  495. networkSettings.IPPrefixLen = ones
  496. if iface.AddressIPv6().IP.To16() != nil {
  497. onesv6, _ := iface.AddressIPv6().Mask.Size()
  498. networkSettings.GlobalIPv6Address = iface.AddressIPv6().IP.String()
  499. networkSettings.GlobalIPv6PrefixLen = onesv6
  500. }
  501. if len(ifaceList) == 1 {
  502. return networkSettings, nil
  503. }
  504. networkSettings.SecondaryIPAddresses = make([]network.Address, 0, len(ifaceList)-1)
  505. networkSettings.SecondaryIPv6Addresses = make([]network.Address, 0, len(ifaceList)-1)
  506. for _, iface := range ifaceList[1:] {
  507. ones, _ := iface.Address().Mask.Size()
  508. addr := network.Address{Addr: iface.Address().IP.String(), PrefixLen: ones}
  509. networkSettings.SecondaryIPAddresses = append(networkSettings.SecondaryIPAddresses, addr)
  510. if iface.AddressIPv6().IP.To16() != nil {
  511. onesv6, _ := iface.AddressIPv6().Mask.Size()
  512. addrv6 := network.Address{Addr: iface.AddressIPv6().IP.String(), PrefixLen: onesv6}
  513. networkSettings.SecondaryIPv6Addresses = append(networkSettings.SecondaryIPv6Addresses, addrv6)
  514. }
  515. }
  516. return networkSettings, nil
  517. }
  518. func (container *Container) updateJoinInfo(ep libnetwork.Endpoint) error {
  519. epInfo := ep.Info()
  520. if epInfo == nil {
  521. // It is not an error to get an empty endpoint info
  522. return nil
  523. }
  524. container.NetworkSettings.Gateway = epInfo.Gateway().String()
  525. if epInfo.GatewayIPv6().To16() != nil {
  526. container.NetworkSettings.IPv6Gateway = epInfo.GatewayIPv6().String()
  527. }
  528. container.NetworkSettings.SandboxKey = epInfo.SandboxKey()
  529. return nil
  530. }
  531. func (container *Container) updateNetworkSettings(n libnetwork.Network, ep libnetwork.Endpoint) error {
  532. networkSettings := &network.Settings{NetworkID: n.ID(), EndpointID: ep.ID()}
  533. networkSettings, err := container.buildPortMapInfo(n, ep, networkSettings)
  534. if err != nil {
  535. return err
  536. }
  537. networkSettings, err = container.buildEndpointInfo(n, ep, networkSettings)
  538. if err != nil {
  539. return err
  540. }
  541. if container.hostConfig.NetworkMode == runconfig.NetworkMode("bridge") {
  542. networkSettings.Bridge = container.daemon.config.Bridge.Iface
  543. }
  544. container.NetworkSettings = networkSettings
  545. return nil
  546. }
  547. // UpdateNetwork is used to update the container's network (e.g. when linked containers
  548. // get removed/unlinked).
  549. func (container *Container) UpdateNetwork() error {
  550. n, err := container.daemon.netController.NetworkByID(container.NetworkSettings.NetworkID)
  551. if err != nil {
  552. return fmt.Errorf("error locating network id %s: %v", container.NetworkSettings.NetworkID, err)
  553. }
  554. ep, err := n.EndpointByID(container.NetworkSettings.EndpointID)
  555. if err != nil {
  556. return fmt.Errorf("error locating endpoint id %s: %v", container.NetworkSettings.EndpointID, err)
  557. }
  558. if err := ep.Leave(container.ID); err != nil {
  559. return fmt.Errorf("endpoint leave failed: %v", err)
  560. }
  561. joinOptions, err := container.buildJoinOptions()
  562. if err != nil {
  563. return fmt.Errorf("Update network failed: %v", err)
  564. }
  565. if err := ep.Join(container.ID, joinOptions...); err != nil {
  566. return fmt.Errorf("endpoint join failed: %v", err)
  567. }
  568. if err := container.updateJoinInfo(ep); err != nil {
  569. return fmt.Errorf("Updating join info failed: %v", err)
  570. }
  571. return nil
  572. }
  573. func (container *Container) buildCreateEndpointOptions() ([]libnetwork.EndpointOption, error) {
  574. var (
  575. portSpecs = make(nat.PortSet)
  576. bindings = make(nat.PortMap)
  577. pbList []types.PortBinding
  578. exposeList []types.TransportPort
  579. createOptions []libnetwork.EndpointOption
  580. )
  581. if container.Config.ExposedPorts != nil {
  582. portSpecs = container.Config.ExposedPorts
  583. }
  584. if container.hostConfig.PortBindings != nil {
  585. for p, b := range container.hostConfig.PortBindings {
  586. bindings[p] = []nat.PortBinding{}
  587. for _, bb := range b {
  588. bindings[p] = append(bindings[p], nat.PortBinding{
  589. HostIP: bb.HostIP,
  590. HostPort: bb.HostPort,
  591. })
  592. }
  593. }
  594. }
  595. container.NetworkSettings.PortMapping = nil
  596. ports := make([]nat.Port, len(portSpecs))
  597. var i int
  598. for p := range portSpecs {
  599. ports[i] = p
  600. i++
  601. }
  602. nat.SortPortMap(ports, bindings)
  603. for _, port := range ports {
  604. expose := types.TransportPort{}
  605. expose.Proto = types.ParseProtocol(port.Proto())
  606. expose.Port = uint16(port.Int())
  607. exposeList = append(exposeList, expose)
  608. pb := types.PortBinding{Port: expose.Port, Proto: expose.Proto}
  609. binding := bindings[port]
  610. for i := 0; i < len(binding); i++ {
  611. pbCopy := pb.GetCopy()
  612. newP, err := nat.NewPort(nat.SplitProtoPort(binding[i].HostPort))
  613. if err != nil {
  614. return nil, fmt.Errorf("Error parsing HostPort value(%s):%v", binding[i].HostPort, err)
  615. }
  616. pbCopy.HostPort = uint16(newP.Int())
  617. pbCopy.HostIP = net.ParseIP(binding[i].HostIP)
  618. pbList = append(pbList, pbCopy)
  619. }
  620. if container.hostConfig.PublishAllPorts && len(binding) == 0 {
  621. pbList = append(pbList, pb)
  622. }
  623. }
  624. createOptions = append(createOptions,
  625. libnetwork.CreateOptionPortMapping(pbList),
  626. libnetwork.CreateOptionExposedPorts(exposeList))
  627. if container.Config.MacAddress != "" {
  628. mac, err := net.ParseMAC(container.Config.MacAddress)
  629. if err != nil {
  630. return nil, err
  631. }
  632. genericOption := options.Generic{
  633. netlabel.MacAddress: mac,
  634. }
  635. createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOption))
  636. }
  637. return createOptions, nil
  638. }
  639. func parseService(controller libnetwork.NetworkController, service string) (string, string, string) {
  640. dn := controller.Config().Daemon.DefaultNetwork
  641. dd := controller.Config().Daemon.DefaultDriver
  642. snd := strings.Split(service, ".")
  643. if len(snd) > 2 {
  644. return strings.Join(snd[:len(snd)-2], "."), snd[len(snd)-2], snd[len(snd)-1]
  645. }
  646. if len(snd) > 1 {
  647. return snd[0], snd[1], dd
  648. }
  649. return snd[0], dn, dd
  650. }
  651. func createNetwork(controller libnetwork.NetworkController, dnet string, driver string) (libnetwork.Network, error) {
  652. createOptions := []libnetwork.NetworkOption{}
  653. genericOption := options.Generic{}
  654. // Bridge driver is special due to legacy reasons
  655. if runconfig.NetworkMode(driver).IsBridge() {
  656. genericOption[netlabel.GenericData] = map[string]interface{}{
  657. "BridgeName": dnet,
  658. "AllowNonDefaultBridge": "true",
  659. }
  660. networkOption := libnetwork.NetworkOptionGeneric(genericOption)
  661. createOptions = append(createOptions, networkOption)
  662. }
  663. return controller.NewNetwork(driver, dnet, createOptions...)
  664. }
  665. func (container *Container) secondaryNetworkRequired(primaryNetworkType string) bool {
  666. switch primaryNetworkType {
  667. case "bridge", "none", "host", "container":
  668. return false
  669. }
  670. if container.daemon.config.DisableBridge {
  671. return false
  672. }
  673. if container.Config.ExposedPorts != nil && len(container.Config.ExposedPorts) > 0 {
  674. return true
  675. }
  676. if container.hostConfig.PortBindings != nil && len(container.hostConfig.PortBindings) > 0 {
  677. return true
  678. }
  679. return false
  680. }
  681. func (container *Container) AllocateNetwork() error {
  682. mode := container.hostConfig.NetworkMode
  683. controller := container.daemon.netController
  684. if container.Config.NetworkDisabled || mode.IsContainer() {
  685. return nil
  686. }
  687. networkDriver := string(mode)
  688. service := container.Config.PublishService
  689. networkName := mode.NetworkName()
  690. if mode.IsDefault() {
  691. if service != "" {
  692. service, networkName, networkDriver = parseService(controller, service)
  693. } else {
  694. networkName = controller.Config().Daemon.DefaultNetwork
  695. networkDriver = controller.Config().Daemon.DefaultDriver
  696. }
  697. } else if service != "" {
  698. return fmt.Errorf("conflicting options: publishing a service and network mode")
  699. }
  700. if runconfig.NetworkMode(networkDriver).IsBridge() && container.daemon.config.DisableBridge {
  701. container.Config.NetworkDisabled = true
  702. return nil
  703. }
  704. if service == "" {
  705. // dot character "." has a special meaning to support SERVICE[.NETWORK] format.
  706. // For backward compatibility, replacing "." with "-", instead of failing
  707. service = strings.Replace(container.Name, ".", "-", -1)
  708. // Service names dont like "/" in them. removing it instead of failing for backward compatibility
  709. service = strings.Replace(service, "/", "", -1)
  710. }
  711. if container.secondaryNetworkRequired(networkDriver) {
  712. // Configure Bridge as secondary network for port binding purposes
  713. if err := container.configureNetwork("bridge", service, "bridge", false); err != nil {
  714. return err
  715. }
  716. }
  717. if err := container.configureNetwork(networkName, service, networkDriver, mode.IsDefault()); err != nil {
  718. return err
  719. }
  720. return container.WriteHostConfig()
  721. }
  722. func (container *Container) configureNetwork(networkName, service, networkDriver string, canCreateNetwork bool) error {
  723. controller := container.daemon.netController
  724. n, err := controller.NetworkByName(networkName)
  725. if err != nil {
  726. if _, ok := err.(libnetwork.ErrNoSuchNetwork); !ok || !canCreateNetwork {
  727. return err
  728. }
  729. if n, err = createNetwork(controller, networkName, networkDriver); err != nil {
  730. return err
  731. }
  732. }
  733. ep, err := n.EndpointByName(service)
  734. if err != nil {
  735. if _, ok := err.(libnetwork.ErrNoSuchEndpoint); !ok {
  736. return err
  737. }
  738. createOptions, err := container.buildCreateEndpointOptions()
  739. if err != nil {
  740. return err
  741. }
  742. ep, err = n.CreateEndpoint(service, createOptions...)
  743. if err != nil {
  744. return err
  745. }
  746. }
  747. if err := container.updateNetworkSettings(n, ep); err != nil {
  748. return err
  749. }
  750. joinOptions, err := container.buildJoinOptions()
  751. if err != nil {
  752. return err
  753. }
  754. if err := ep.Join(container.ID, joinOptions...); err != nil {
  755. return err
  756. }
  757. if err := container.updateJoinInfo(ep); err != nil {
  758. return fmt.Errorf("Updating join info failed: %v", err)
  759. }
  760. return nil
  761. }
  762. func (container *Container) initializeNetworking() error {
  763. var err error
  764. if container.hostConfig.NetworkMode.IsContainer() {
  765. // we need to get the hosts files from the container to join
  766. nc, err := container.getNetworkedContainer()
  767. if err != nil {
  768. return err
  769. }
  770. container.HostnamePath = nc.HostnamePath
  771. container.HostsPath = nc.HostsPath
  772. container.ResolvConfPath = nc.ResolvConfPath
  773. container.Config.Hostname = nc.Config.Hostname
  774. container.Config.Domainname = nc.Config.Domainname
  775. return nil
  776. }
  777. if container.hostConfig.NetworkMode.IsHost() {
  778. container.Config.Hostname, err = os.Hostname()
  779. if err != nil {
  780. return err
  781. }
  782. parts := strings.SplitN(container.Config.Hostname, ".", 2)
  783. if len(parts) > 1 {
  784. container.Config.Hostname = parts[0]
  785. container.Config.Domainname = parts[1]
  786. }
  787. }
  788. if err := container.AllocateNetwork(); err != nil {
  789. return err
  790. }
  791. return container.buildHostnameFile()
  792. }
  793. func (container *Container) getIpcContainer() (*Container, error) {
  794. containerID := container.hostConfig.IpcMode.Container()
  795. c, err := container.daemon.Get(containerID)
  796. if err != nil {
  797. return nil, err
  798. }
  799. if !c.IsRunning() {
  800. return nil, fmt.Errorf("cannot join IPC of a non running container: %s", containerID)
  801. }
  802. return c, nil
  803. }
  804. func (container *Container) setupWorkingDirectory() error {
  805. if container.Config.WorkingDir != "" {
  806. container.Config.WorkingDir = filepath.Clean(container.Config.WorkingDir)
  807. pth, err := container.GetResourcePath(container.Config.WorkingDir)
  808. if err != nil {
  809. return err
  810. }
  811. pthInfo, err := os.Stat(pth)
  812. if err != nil {
  813. if !os.IsNotExist(err) {
  814. return err
  815. }
  816. if err := system.MkdirAll(pth, 0755); err != nil {
  817. return err
  818. }
  819. }
  820. if pthInfo != nil && !pthInfo.IsDir() {
  821. return fmt.Errorf("Cannot mkdir: %s is not a directory", container.Config.WorkingDir)
  822. }
  823. }
  824. return nil
  825. }
  826. func (container *Container) getNetworkedContainer() (*Container, error) {
  827. parts := strings.SplitN(string(container.hostConfig.NetworkMode), ":", 2)
  828. switch parts[0] {
  829. case "container":
  830. if len(parts) != 2 {
  831. return nil, fmt.Errorf("no container specified to join network")
  832. }
  833. nc, err := container.daemon.Get(parts[1])
  834. if err != nil {
  835. return nil, err
  836. }
  837. if container == nc {
  838. return nil, fmt.Errorf("cannot join own network")
  839. }
  840. if !nc.IsRunning() {
  841. return nil, fmt.Errorf("cannot join network of a non running container: %s", parts[1])
  842. }
  843. return nc, nil
  844. default:
  845. return nil, fmt.Errorf("network mode not set to container")
  846. }
  847. }
  848. func (container *Container) ReleaseNetwork() {
  849. if container.hostConfig.NetworkMode.IsContainer() || container.Config.NetworkDisabled {
  850. return
  851. }
  852. eid := container.NetworkSettings.EndpointID
  853. nid := container.NetworkSettings.NetworkID
  854. container.NetworkSettings = &network.Settings{}
  855. if nid == "" || eid == "" {
  856. return
  857. }
  858. n, err := container.daemon.netController.NetworkByID(nid)
  859. if err != nil {
  860. logrus.Errorf("error locating network id %s: %v", nid, err)
  861. return
  862. }
  863. ep, err := n.EndpointByID(eid)
  864. if err != nil {
  865. logrus.Errorf("error locating endpoint id %s: %v", eid, err)
  866. return
  867. }
  868. switch {
  869. case container.hostConfig.NetworkMode.IsHost():
  870. if err := ep.Leave(container.ID); err != nil {
  871. logrus.Errorf("Error leaving endpoint id %s for container %s: %v", eid, container.ID, err)
  872. return
  873. }
  874. default:
  875. if err := container.daemon.netController.LeaveAll(container.ID); err != nil {
  876. logrus.Errorf("Leave all failed for %s: %v", container.ID, err)
  877. return
  878. }
  879. }
  880. // In addition to leaving all endpoints, delete implicitly created endpoint
  881. if container.Config.PublishService == "" {
  882. if err := ep.Delete(); err != nil {
  883. logrus.Errorf("deleting endpoint failed: %v", err)
  884. }
  885. }
  886. }
  887. func (container *Container) UnmountVolumes(forceSyscall bool) error {
  888. var volumeMounts []mountPoint
  889. for _, mntPoint := range container.MountPoints {
  890. dest, err := container.GetResourcePath(mntPoint.Destination)
  891. if err != nil {
  892. return err
  893. }
  894. volumeMounts = append(volumeMounts, mountPoint{Destination: dest, Volume: mntPoint.Volume})
  895. }
  896. for _, mnt := range container.networkMounts() {
  897. dest, err := container.GetResourcePath(mnt.Destination)
  898. if err != nil {
  899. return err
  900. }
  901. volumeMounts = append(volumeMounts, mountPoint{Destination: dest})
  902. }
  903. for _, volumeMount := range volumeMounts {
  904. if forceSyscall {
  905. syscall.Unmount(volumeMount.Destination, 0)
  906. }
  907. if volumeMount.Volume != nil {
  908. if err := volumeMount.Volume.Unmount(); err != nil {
  909. return err
  910. }
  911. }
  912. }
  913. return nil
  914. }
  915. func (container *Container) networkMounts() []execdriver.Mount {
  916. var mounts []execdriver.Mount
  917. mode := "Z"
  918. if container.hostConfig.NetworkMode.IsContainer() {
  919. mode = "z"
  920. }
  921. if container.ResolvConfPath != "" {
  922. label.Relabel(container.ResolvConfPath, container.MountLabel, mode)
  923. writable := !container.hostConfig.ReadonlyRootfs
  924. if m, exists := container.MountPoints["/etc/resolv.conf"]; exists {
  925. writable = m.RW
  926. }
  927. mounts = append(mounts, execdriver.Mount{
  928. Source: container.ResolvConfPath,
  929. Destination: "/etc/resolv.conf",
  930. Writable: writable,
  931. Private: true,
  932. })
  933. }
  934. if container.HostnamePath != "" {
  935. label.Relabel(container.HostnamePath, container.MountLabel, mode)
  936. writable := !container.hostConfig.ReadonlyRootfs
  937. if m, exists := container.MountPoints["/etc/hostname"]; exists {
  938. writable = m.RW
  939. }
  940. mounts = append(mounts, execdriver.Mount{
  941. Source: container.HostnamePath,
  942. Destination: "/etc/hostname",
  943. Writable: writable,
  944. Private: true,
  945. })
  946. }
  947. if container.HostsPath != "" {
  948. label.Relabel(container.HostsPath, container.MountLabel, mode)
  949. writable := !container.hostConfig.ReadonlyRootfs
  950. if m, exists := container.MountPoints["/etc/hosts"]; exists {
  951. writable = m.RW
  952. }
  953. mounts = append(mounts, execdriver.Mount{
  954. Source: container.HostsPath,
  955. Destination: "/etc/hosts",
  956. Writable: writable,
  957. Private: true,
  958. })
  959. }
  960. return mounts
  961. }
  962. func (container *Container) addBindMountPoint(name, source, destination string, rw bool) {
  963. container.MountPoints[destination] = &mountPoint{
  964. Name: name,
  965. Source: source,
  966. Destination: destination,
  967. RW: rw,
  968. }
  969. }
  970. func (container *Container) addLocalMountPoint(name, destination string, rw bool) {
  971. container.MountPoints[destination] = &mountPoint{
  972. Name: name,
  973. Driver: volume.DefaultDriverName,
  974. Destination: destination,
  975. RW: rw,
  976. }
  977. }
  978. func (container *Container) addMountPointWithVolume(destination string, vol volume.Volume, rw bool) {
  979. container.MountPoints[destination] = &mountPoint{
  980. Name: vol.Name(),
  981. Driver: vol.DriverName(),
  982. Destination: destination,
  983. RW: rw,
  984. Volume: vol,
  985. }
  986. }
  987. func (container *Container) isDestinationMounted(destination string) bool {
  988. return container.MountPoints[destination] != nil
  989. }
  990. func (container *Container) prepareMountPoints() error {
  991. for _, config := range container.MountPoints {
  992. if len(config.Driver) > 0 {
  993. v, err := createVolume(config.Name, config.Driver)
  994. if err != nil {
  995. return err
  996. }
  997. config.Volume = v
  998. }
  999. }
  1000. return nil
  1001. }
  1002. func (container *Container) removeMountPoints() error {
  1003. for _, m := range container.MountPoints {
  1004. if m.Volume != nil {
  1005. if err := removeVolume(m.Volume); err != nil {
  1006. return err
  1007. }
  1008. }
  1009. }
  1010. return nil
  1011. }