daemon_unix.go 19 KB

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