daemon_unix.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // +build linux freebsd
  2. package daemon
  3. import (
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "syscall"
  11. "github.com/Sirupsen/logrus"
  12. "github.com/docker/docker/autogen/dockerversion"
  13. "github.com/docker/docker/daemon/graphdriver"
  14. "github.com/docker/docker/pkg/fileutils"
  15. "github.com/docker/docker/pkg/parsers"
  16. "github.com/docker/docker/pkg/parsers/kernel"
  17. "github.com/docker/docker/pkg/sysinfo"
  18. "github.com/docker/docker/pkg/system"
  19. "github.com/docker/docker/runconfig"
  20. "github.com/docker/docker/utils"
  21. "github.com/docker/libnetwork"
  22. nwapi "github.com/docker/libnetwork/api"
  23. nwconfig "github.com/docker/libnetwork/config"
  24. "github.com/docker/libnetwork/netlabel"
  25. "github.com/docker/libnetwork/options"
  26. "github.com/opencontainers/runc/libcontainer/label"
  27. )
  28. const (
  29. // See https://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/tree/kernel/sched/sched.h?id=8cd9234c64c584432f6992fe944ca9e46ca8ea76#n269
  30. linuxMinCPUShares = 2
  31. linuxMaxCPUShares = 262144
  32. platformSupported = true
  33. )
  34. func parseSecurityOpt(container *Container, config *runconfig.HostConfig) error {
  35. var (
  36. labelOpts []string
  37. err error
  38. )
  39. for _, opt := range config.SecurityOpt {
  40. con := strings.SplitN(opt, ":", 2)
  41. if len(con) == 1 {
  42. return fmt.Errorf("Invalid --security-opt: %q", opt)
  43. }
  44. switch con[0] {
  45. case "label":
  46. labelOpts = append(labelOpts, con[1])
  47. case "apparmor":
  48. container.AppArmorProfile = con[1]
  49. default:
  50. return fmt.Errorf("Invalid --security-opt: %q", opt)
  51. }
  52. }
  53. container.ProcessLabel, container.MountLabel, err = label.InitLabels(labelOpts)
  54. return err
  55. }
  56. func checkKernelVersion(k, major, minor int) bool {
  57. if v, err := kernel.GetKernelVersion(); err != nil {
  58. logrus.Warnf("%s", err)
  59. } else {
  60. if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: k, Major: major, Minor: minor}) < 0 {
  61. return false
  62. }
  63. }
  64. return true
  65. }
  66. func checkKernel() error {
  67. // Check for unsupported kernel versions
  68. // FIXME: it would be cleaner to not test for specific versions, but rather
  69. // test for specific functionalities.
  70. // Unfortunately we can't test for the feature "does not cause a kernel panic"
  71. // without actually causing a kernel panic, so we need this workaround until
  72. // the circumstances of pre-3.10 crashes are clearer.
  73. // For details see https://github.com/docker/docker/issues/407
  74. if !checkKernelVersion(3, 10, 0) {
  75. v, _ := kernel.GetKernelVersion()
  76. if os.Getenv("DOCKER_NOWARN_KERNEL_VERSION") == "" {
  77. logrus.Warnf("Your Linux kernel version %s can be unstable running docker. Please upgrade your kernel to 3.10.0.", v.String())
  78. }
  79. }
  80. return nil
  81. }
  82. // adaptContainerSettings is called during container creation to modify any
  83. // settings necessary in the HostConfig structure.
  84. func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) {
  85. if hostConfig == nil {
  86. return
  87. }
  88. if adjustCPUShares && hostConfig.CPUShares > 0 {
  89. // Handle unsupported CPUShares
  90. if hostConfig.CPUShares < linuxMinCPUShares {
  91. logrus.Warnf("Changing requested CPUShares of %d to minimum allowed of %d", hostConfig.CPUShares, linuxMinCPUShares)
  92. hostConfig.CPUShares = linuxMinCPUShares
  93. } else if hostConfig.CPUShares > linuxMaxCPUShares {
  94. logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, linuxMaxCPUShares)
  95. hostConfig.CPUShares = linuxMaxCPUShares
  96. }
  97. }
  98. if hostConfig.Memory > 0 && hostConfig.MemorySwap == 0 {
  99. // By default, MemorySwap is set to twice the size of Memory.
  100. hostConfig.MemorySwap = hostConfig.Memory * 2
  101. }
  102. if hostConfig.MemoryReservation == 0 && hostConfig.Memory > 0 {
  103. hostConfig.MemoryReservation = hostConfig.Memory
  104. }
  105. }
  106. // verifyPlatformContainerSettings performs platform-specific validation of the
  107. // hostconfig and config structures.
  108. func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *runconfig.HostConfig, config *runconfig.Config) ([]string, error) {
  109. warnings := []string{}
  110. sysInfo := sysinfo.New(true)
  111. if hostConfig.LxcConf.Len() > 0 && !strings.Contains(daemon.ExecutionDriver().Name(), "lxc") {
  112. return warnings, fmt.Errorf("Cannot use --lxc-conf with execdriver: %s", daemon.ExecutionDriver().Name())
  113. }
  114. // memory subsystem checks and adjustments
  115. if hostConfig.Memory != 0 && hostConfig.Memory < 4194304 {
  116. return warnings, fmt.Errorf("Minimum memory limit allowed is 4MB")
  117. }
  118. if hostConfig.Memory > 0 && !sysInfo.MemoryLimit {
  119. warnings = append(warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.")
  120. logrus.Warnf("Your kernel does not support memory limit capabilities. Limitation discarded.")
  121. hostConfig.Memory = 0
  122. hostConfig.MemorySwap = -1
  123. }
  124. if hostConfig.Memory > 0 && hostConfig.MemorySwap != -1 && !sysInfo.SwapLimit {
  125. warnings = append(warnings, "Your kernel does not support swap limit capabilities, memory limited without swap.")
  126. logrus.Warnf("Your kernel does not support swap limit capabilities, memory limited without swap.")
  127. hostConfig.MemorySwap = -1
  128. }
  129. if hostConfig.Memory > 0 && hostConfig.MemorySwap > 0 && hostConfig.MemorySwap < hostConfig.Memory {
  130. return warnings, fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.")
  131. }
  132. if hostConfig.Memory == 0 && hostConfig.MemorySwap > 0 {
  133. return warnings, fmt.Errorf("You should always set the Memory limit when using Memoryswap limit, see usage.")
  134. }
  135. if hostConfig.MemorySwappiness != nil && *hostConfig.MemorySwappiness != -1 && !sysInfo.MemorySwappiness {
  136. warnings = append(warnings, "Your kernel does not support memory swappiness capabilities, memory swappiness discarded.")
  137. logrus.Warnf("Your kernel does not support memory swappiness capabilities, memory swappiness discarded.")
  138. hostConfig.MemorySwappiness = nil
  139. }
  140. if hostConfig.MemorySwappiness != nil {
  141. swappiness := *hostConfig.MemorySwappiness
  142. if swappiness < -1 || swappiness > 100 {
  143. return warnings, fmt.Errorf("Invalid value: %v, valid memory swappiness range is 0-100.", swappiness)
  144. }
  145. }
  146. if hostConfig.MemoryReservation > 0 && !sysInfo.MemoryReservation {
  147. warnings = append(warnings, "Your kernel does not support memory soft limit capabilities. Limitation discarded.")
  148. logrus.Warnf("Your kernel does not support memory soft limit capabilities. Limitation discarded.")
  149. hostConfig.MemoryReservation = 0
  150. }
  151. if hostConfig.Memory > 0 && hostConfig.MemoryReservation > 0 && hostConfig.Memory < hostConfig.MemoryReservation {
  152. return warnings, fmt.Errorf("Minimum memory limit should be larger than memory reservation limit, see usage.")
  153. }
  154. if hostConfig.KernelMemory > 0 && !sysInfo.KernelMemory {
  155. warnings = append(warnings, "Your kernel does not support kernel memory limit capabilities. Limitation discarded.")
  156. logrus.Warnf("Your kernel does not support kernel memory limit capabilities. Limitation discarded.")
  157. hostConfig.KernelMemory = 0
  158. }
  159. if hostConfig.KernelMemory > 0 && !checkKernelVersion(4, 0, 0) {
  160. warnings = append(warnings, "You specified a kernel memory limit on a kernel older than 4.0. Kernel memory limits are experimental on older kernels, it won't work as expected and can cause your system to be unstable.")
  161. logrus.Warnf("You specified a kernel memory limit on a kernel older than 4.0. Kernel memory limits are experimental on older kernels, it won't work as expected and can cause your system to be unstable.")
  162. }
  163. if hostConfig.CPUShares > 0 && !sysInfo.CPUShares {
  164. warnings = append(warnings, "Your kernel does not support CPU shares. Shares discarded.")
  165. logrus.Warnf("Your kernel does not support CPU shares. Shares discarded.")
  166. hostConfig.CPUShares = 0
  167. }
  168. if hostConfig.CPUPeriod > 0 && !sysInfo.CPUCfsPeriod {
  169. warnings = append(warnings, "Your kernel does not support CPU cfs period. Period discarded.")
  170. logrus.Warnf("Your kernel does not support CPU cfs period. Period discarded.")
  171. hostConfig.CPUPeriod = 0
  172. }
  173. if hostConfig.CPUQuota > 0 && !sysInfo.CPUCfsQuota {
  174. warnings = append(warnings, "Your kernel does not support CPU cfs quota. Quota discarded.")
  175. logrus.Warnf("Your kernel does not support CPU cfs quota. Quota discarded.")
  176. hostConfig.CPUQuota = 0
  177. }
  178. if (hostConfig.CpusetCpus != "" || hostConfig.CpusetMems != "") && !sysInfo.Cpuset {
  179. warnings = append(warnings, "Your kernel does not support cpuset. Cpuset discarded.")
  180. logrus.Warnf("Your kernel does not support cpuset. Cpuset discarded.")
  181. hostConfig.CpusetCpus = ""
  182. hostConfig.CpusetMems = ""
  183. }
  184. if hostConfig.BlkioWeight > 0 && !sysInfo.BlkioWeight {
  185. warnings = append(warnings, "Your kernel does not support Block I/O weight. Weight discarded.")
  186. logrus.Warnf("Your kernel does not support Block I/O weight. Weight discarded.")
  187. hostConfig.BlkioWeight = 0
  188. }
  189. if hostConfig.BlkioWeight > 0 && (hostConfig.BlkioWeight < 10 || hostConfig.BlkioWeight > 1000) {
  190. return warnings, fmt.Errorf("Range of blkio weight is from 10 to 1000.")
  191. }
  192. if hostConfig.OomKillDisable && !sysInfo.OomKillDisable {
  193. hostConfig.OomKillDisable = false
  194. return warnings, fmt.Errorf("Your kernel does not support oom kill disable.")
  195. }
  196. if sysInfo.IPv4ForwardingDisabled {
  197. warnings = append(warnings, "IPv4 forwarding is disabled. Networking will not work.")
  198. logrus.Warnf("IPv4 forwarding is disabled. Networking will not work")
  199. }
  200. return warnings, nil
  201. }
  202. // checkConfigOptions checks for mutually incompatible config options
  203. func checkConfigOptions(config *Config) error {
  204. // Check for mutually incompatible config options
  205. if config.Bridge.Iface != "" && config.Bridge.IP != "" {
  206. return fmt.Errorf("You specified -b & --bip, mutually exclusive options. Please specify only one.")
  207. }
  208. if !config.Bridge.EnableIPTables && !config.Bridge.InterContainerCommunication {
  209. return fmt.Errorf("You specified --iptables=false with --icc=false. ICC uses iptables to function. Please set --icc or --iptables to true.")
  210. }
  211. if !config.Bridge.EnableIPTables && config.Bridge.EnableIPMasq {
  212. config.Bridge.EnableIPMasq = false
  213. }
  214. return nil
  215. }
  216. // checkSystem validates platform-specific requirements
  217. func checkSystem() error {
  218. if os.Geteuid() != 0 {
  219. return fmt.Errorf("The Docker daemon needs to be run as root")
  220. }
  221. if err := checkKernel(); err != nil {
  222. return err
  223. }
  224. return nil
  225. }
  226. // configureKernelSecuritySupport configures and validate security support for the kernel
  227. func configureKernelSecuritySupport(config *Config, driverName string) error {
  228. if config.EnableSelinuxSupport {
  229. if selinuxEnabled() {
  230. // As Docker on either btrfs or overlayFS and SELinux are incompatible at present, error on both being enabled
  231. if driverName == "btrfs" || driverName == "overlay" {
  232. return fmt.Errorf("SELinux is not supported with the %s graph driver", driverName)
  233. }
  234. logrus.Debug("SELinux enabled successfully")
  235. } else {
  236. logrus.Warn("Docker could not enable SELinux on the host system")
  237. }
  238. } else {
  239. selinuxSetDisabled()
  240. }
  241. return nil
  242. }
  243. // MigrateIfDownlevel is a wrapper for AUFS migration for downlevel
  244. func migrateIfDownlevel(driver graphdriver.Driver, root string) error {
  245. return migrateIfAufs(driver, root)
  246. }
  247. func configureSysInit(config *Config) (string, error) {
  248. localCopy := filepath.Join(config.Root, "init", fmt.Sprintf("dockerinit-%s", dockerversion.VERSION))
  249. sysInitPath := utils.DockerInitPath(localCopy)
  250. if sysInitPath == "" {
  251. return "", fmt.Errorf("Could not locate dockerinit: This usually means docker was built incorrectly. See https://docs.docker.com/contributing/devenvironment for official build instructions.")
  252. }
  253. if sysInitPath != localCopy {
  254. // When we find a suitable dockerinit binary (even if it's our local binary), we copy it into config.Root at localCopy for future use (so that the original can go away without that being a problem, for example during a package upgrade).
  255. if err := os.Mkdir(filepath.Dir(localCopy), 0700); err != nil && !os.IsExist(err) {
  256. return "", err
  257. }
  258. if _, err := fileutils.CopyFile(sysInitPath, localCopy); err != nil {
  259. return "", err
  260. }
  261. if err := os.Chmod(localCopy, 0700); err != nil {
  262. return "", err
  263. }
  264. sysInitPath = localCopy
  265. }
  266. return sysInitPath, nil
  267. }
  268. func isBridgeNetworkDisabled(config *Config) bool {
  269. return config.Bridge.Iface == disableNetworkBridge
  270. }
  271. func networkOptions(dconfig *Config) ([]nwconfig.Option, error) {
  272. options := []nwconfig.Option{}
  273. if dconfig == nil {
  274. return options, nil
  275. }
  276. if strings.TrimSpace(dconfig.DefaultNetwork) != "" {
  277. dn := strings.Split(dconfig.DefaultNetwork, ":")
  278. if len(dn) < 2 {
  279. return nil, fmt.Errorf("default network daemon config must be of the form NETWORKDRIVER:NETWORKNAME")
  280. }
  281. options = append(options, nwconfig.OptionDefaultDriver(dn[0]))
  282. options = append(options, nwconfig.OptionDefaultNetwork(strings.Join(dn[1:], ":")))
  283. } else {
  284. dd := runconfig.DefaultDaemonNetworkMode()
  285. dn := runconfig.DefaultDaemonNetworkMode().NetworkName()
  286. options = append(options, nwconfig.OptionDefaultDriver(string(dd)))
  287. options = append(options, nwconfig.OptionDefaultNetwork(dn))
  288. }
  289. if strings.TrimSpace(dconfig.NetworkKVStore) != "" {
  290. kv := strings.Split(dconfig.NetworkKVStore, ":")
  291. if len(kv) < 2 {
  292. return nil, fmt.Errorf("kv store daemon config must be of the form KV-PROVIDER:KV-URL")
  293. }
  294. options = append(options, nwconfig.OptionKVProvider(kv[0]))
  295. options = append(options, nwconfig.OptionKVProviderURL(strings.Join(kv[1:], ":")))
  296. }
  297. options = append(options, nwconfig.OptionLabels(dconfig.Labels))
  298. return options, nil
  299. }
  300. func initNetworkController(config *Config) (libnetwork.NetworkController, error) {
  301. netOptions, err := networkOptions(config)
  302. if err != nil {
  303. return nil, err
  304. }
  305. controller, err := libnetwork.New(netOptions...)
  306. if err != nil {
  307. return nil, fmt.Errorf("error obtaining controller instance: %v", err)
  308. }
  309. // Initialize default driver "null"
  310. if err := controller.ConfigureNetworkDriver("null", options.Generic{}); err != nil {
  311. return nil, fmt.Errorf("Error initializing null driver: %v", err)
  312. }
  313. // Initialize default network on "null"
  314. if _, err := controller.NewNetwork("null", "none"); err != nil {
  315. return nil, fmt.Errorf("Error creating default \"null\" network: %v", err)
  316. }
  317. // Initialize default driver "host"
  318. if err := controller.ConfigureNetworkDriver("host", options.Generic{}); err != nil {
  319. return nil, fmt.Errorf("Error initializing host driver: %v", err)
  320. }
  321. // Initialize default network on "host"
  322. if _, err := controller.NewNetwork("host", "host"); err != nil {
  323. return nil, fmt.Errorf("Error creating default \"host\" network: %v", err)
  324. }
  325. if !config.DisableBridge {
  326. // Initialize default driver "bridge"
  327. if err := initBridgeDriver(controller, config); err != nil {
  328. return nil, err
  329. }
  330. }
  331. return controller, nil
  332. }
  333. func initBridgeDriver(controller libnetwork.NetworkController, config *Config) error {
  334. option := options.Generic{
  335. "EnableIPForwarding": config.Bridge.EnableIPForward,
  336. "EnableIPTables": config.Bridge.EnableIPTables,
  337. "EnableUserlandProxy": config.Bridge.EnableUserlandProxy}
  338. if err := controller.ConfigureNetworkDriver("bridge", options.Generic{netlabel.GenericData: option}); err != nil {
  339. return fmt.Errorf("Error initializing bridge driver: %v", err)
  340. }
  341. netOption := options.Generic{
  342. "BridgeName": config.Bridge.Iface,
  343. "Mtu": config.Mtu,
  344. "EnableIPMasquerade": config.Bridge.EnableIPMasq,
  345. "EnableICC": config.Bridge.InterContainerCommunication,
  346. }
  347. if config.Bridge.IP != "" {
  348. ip, bipNet, err := net.ParseCIDR(config.Bridge.IP)
  349. if err != nil {
  350. return err
  351. }
  352. bipNet.IP = ip
  353. netOption["AddressIPv4"] = bipNet
  354. }
  355. if config.Bridge.FixedCIDR != "" {
  356. _, fCIDR, err := net.ParseCIDR(config.Bridge.FixedCIDR)
  357. if err != nil {
  358. return err
  359. }
  360. netOption["FixedCIDR"] = fCIDR
  361. }
  362. if config.Bridge.FixedCIDRv6 != "" {
  363. _, fCIDRv6, err := net.ParseCIDR(config.Bridge.FixedCIDRv6)
  364. if err != nil {
  365. return err
  366. }
  367. netOption["FixedCIDRv6"] = fCIDRv6
  368. }
  369. if config.Bridge.DefaultGatewayIPv4 != nil {
  370. netOption["DefaultGatewayIPv4"] = config.Bridge.DefaultGatewayIPv4
  371. }
  372. if config.Bridge.DefaultGatewayIPv6 != nil {
  373. netOption["DefaultGatewayIPv6"] = config.Bridge.DefaultGatewayIPv6
  374. }
  375. // --ip processing
  376. if config.Bridge.DefaultIP != nil {
  377. netOption["DefaultBindingIP"] = config.Bridge.DefaultIP
  378. }
  379. // Initialize default network on "bridge" with the same name
  380. _, err := controller.NewNetwork("bridge", "bridge",
  381. libnetwork.NetworkOptionGeneric(options.Generic{
  382. netlabel.GenericData: netOption,
  383. netlabel.EnableIPv6: config.Bridge.EnableIPv6,
  384. }))
  385. if err != nil {
  386. return fmt.Errorf("Error creating default \"bridge\" network: %v", err)
  387. }
  388. return nil
  389. }
  390. // setupInitLayer populates a directory with mountpoints suitable
  391. // for bind-mounting dockerinit into the container. The mountpoint is simply an
  392. // empty file at /.dockerinit
  393. //
  394. // This extra layer is used by all containers as the top-most ro layer. It protects
  395. // the container from unwanted side-effects on the rw layer.
  396. func setupInitLayer(initLayer string) error {
  397. for pth, typ := range map[string]string{
  398. "/dev/pts": "dir",
  399. "/dev/shm": "dir",
  400. "/proc": "dir",
  401. "/sys": "dir",
  402. "/.dockerinit": "file",
  403. "/.dockerenv": "file",
  404. "/etc/resolv.conf": "file",
  405. "/etc/hosts": "file",
  406. "/etc/hostname": "file",
  407. "/dev/console": "file",
  408. "/etc/mtab": "/proc/mounts",
  409. } {
  410. parts := strings.Split(pth, "/")
  411. prev := "/"
  412. for _, p := range parts[1:] {
  413. prev = filepath.Join(prev, p)
  414. syscall.Unlink(filepath.Join(initLayer, prev))
  415. }
  416. if _, err := os.Stat(filepath.Join(initLayer, pth)); err != nil {
  417. if os.IsNotExist(err) {
  418. if err := system.MkdirAll(filepath.Join(initLayer, filepath.Dir(pth)), 0755); err != nil {
  419. return err
  420. }
  421. switch typ {
  422. case "dir":
  423. if err := system.MkdirAll(filepath.Join(initLayer, pth), 0755); err != nil {
  424. return err
  425. }
  426. case "file":
  427. f, err := os.OpenFile(filepath.Join(initLayer, pth), os.O_CREATE, 0755)
  428. if err != nil {
  429. return err
  430. }
  431. f.Close()
  432. default:
  433. if err := os.Symlink(typ, filepath.Join(initLayer, pth)); err != nil {
  434. return err
  435. }
  436. }
  437. } else {
  438. return err
  439. }
  440. }
  441. }
  442. // Layer is ready to use, if it wasn't before.
  443. return nil
  444. }
  445. // NetworkAPIRouter implements a feature for server-experimental,
  446. // directly calling into libnetwork.
  447. func (daemon *Daemon) NetworkAPIRouter() func(w http.ResponseWriter, req *http.Request) {
  448. return nwapi.NewHTTPHandler(daemon.netController)
  449. }
  450. // registerLinks writes the links to a file.
  451. func (daemon *Daemon) registerLinks(container *Container, hostConfig *runconfig.HostConfig) error {
  452. if hostConfig == nil || hostConfig.Links == nil {
  453. return nil
  454. }
  455. for _, l := range hostConfig.Links {
  456. name, alias, err := parsers.ParseLink(l)
  457. if err != nil {
  458. return err
  459. }
  460. child, err := daemon.Get(name)
  461. if err != nil {
  462. //An error from daemon.Get() means this name could not be found
  463. return fmt.Errorf("Could not get container for %s", name)
  464. }
  465. for child.hostConfig.NetworkMode.IsContainer() {
  466. parts := strings.SplitN(string(child.hostConfig.NetworkMode), ":", 2)
  467. child, err = daemon.Get(parts[1])
  468. if err != nil {
  469. return fmt.Errorf("Could not get container for %s", parts[1])
  470. }
  471. }
  472. if child.hostConfig.NetworkMode.IsHost() {
  473. return runconfig.ErrConflictHostNetworkAndLinks
  474. }
  475. if err := daemon.registerLink(container, child, alias); err != nil {
  476. return err
  477. }
  478. }
  479. // After we load all the links into the daemon
  480. // set them to nil on the hostconfig
  481. hostConfig.Links = nil
  482. if err := container.writeHostConfig(); err != nil {
  483. return err
  484. }
  485. return nil
  486. }
  487. func (daemon *Daemon) newBaseContainer(id string) Container {
  488. return Container{
  489. CommonContainer: CommonContainer{
  490. ID: id,
  491. State: NewState(),
  492. execCommands: newExecStore(),
  493. root: daemon.containerRoot(id),
  494. },
  495. MountPoints: make(map[string]*mountPoint),
  496. Volumes: make(map[string]string),
  497. VolumesRW: make(map[string]bool),
  498. }
  499. }