config_linux.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package config // import "github.com/docker/docker/daemon/config"
  2. import (
  3. "fmt"
  4. "net"
  5. "os/exec"
  6. "path/filepath"
  7. "github.com/containerd/cgroups"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/opts"
  11. "github.com/docker/docker/pkg/homedir"
  12. "github.com/docker/docker/rootless"
  13. units "github.com/docker/go-units"
  14. "github.com/pkg/errors"
  15. )
  16. const (
  17. // DefaultIpcMode is default for container's IpcMode, if not set otherwise
  18. DefaultIpcMode = container.IPCModePrivate
  19. // DefaultCgroupNamespaceMode is the default mode for containers cgroup namespace when using cgroups v2.
  20. DefaultCgroupNamespaceMode = container.CgroupnsModePrivate
  21. // DefaultCgroupV1NamespaceMode is the default mode for containers cgroup namespace when using cgroups v1.
  22. DefaultCgroupV1NamespaceMode = container.CgroupnsModeHost
  23. // StockRuntimeName is the reserved name/alias used to represent the
  24. // OCI runtime being shipped with the docker daemon package.
  25. StockRuntimeName = "runc"
  26. )
  27. // BridgeConfig stores all the bridge driver specific
  28. // configuration.
  29. type BridgeConfig struct {
  30. commonBridgeConfig
  31. // Fields below here are platform specific.
  32. DefaultIP net.IP `json:"ip,omitempty"`
  33. IP string `json:"bip,omitempty"`
  34. DefaultGatewayIPv4 net.IP `json:"default-gateway,omitempty"`
  35. DefaultGatewayIPv6 net.IP `json:"default-gateway-v6,omitempty"`
  36. InterContainerCommunication bool `json:"icc,omitempty"`
  37. EnableIPv6 bool `json:"ipv6,omitempty"`
  38. EnableIPTables bool `json:"iptables,omitempty"`
  39. EnableIP6Tables bool `json:"ip6tables,omitempty"`
  40. EnableIPForward bool `json:"ip-forward,omitempty"`
  41. EnableIPMasq bool `json:"ip-masq,omitempty"`
  42. EnableUserlandProxy bool `json:"userland-proxy,omitempty"`
  43. UserlandProxyPath string `json:"userland-proxy-path,omitempty"`
  44. FixedCIDRv6 string `json:"fixed-cidr-v6,omitempty"`
  45. }
  46. // Config defines the configuration of a docker daemon.
  47. // It includes json tags to deserialize configuration from a file
  48. // using the same names that the flags in the command line uses.
  49. type Config struct {
  50. CommonConfig
  51. // Fields below here are platform specific.
  52. Runtimes map[string]types.Runtime `json:"runtimes,omitempty"`
  53. DefaultInitBinary string `json:"default-init,omitempty"`
  54. CgroupParent string `json:"cgroup-parent,omitempty"`
  55. EnableSelinuxSupport bool `json:"selinux-enabled,omitempty"`
  56. RemappedRoot string `json:"userns-remap,omitempty"`
  57. Ulimits map[string]*units.Ulimit `json:"default-ulimits,omitempty"`
  58. CPURealtimePeriod int64 `json:"cpu-rt-period,omitempty"`
  59. CPURealtimeRuntime int64 `json:"cpu-rt-runtime,omitempty"`
  60. OOMScoreAdjust int `json:"oom-score-adjust,omitempty"`
  61. Init bool `json:"init,omitempty"`
  62. InitPath string `json:"init-path,omitempty"`
  63. SeccompProfile string `json:"seccomp-profile,omitempty"`
  64. ShmSize opts.MemBytes `json:"default-shm-size,omitempty"`
  65. NoNewPrivileges bool `json:"no-new-privileges,omitempty"`
  66. IpcMode string `json:"default-ipc-mode,omitempty"`
  67. CgroupNamespaceMode string `json:"default-cgroupns-mode,omitempty"`
  68. // ResolvConf is the path to the configuration of the host resolver
  69. ResolvConf string `json:"resolv-conf,omitempty"`
  70. Rootless bool `json:"rootless,omitempty"`
  71. }
  72. // GetRuntime returns the runtime path and arguments for a given
  73. // runtime name
  74. func (conf *Config) GetRuntime(name string) *types.Runtime {
  75. conf.Lock()
  76. defer conf.Unlock()
  77. if rt, ok := conf.Runtimes[name]; ok {
  78. return &rt
  79. }
  80. return nil
  81. }
  82. // GetAllRuntimes returns a copy of the runtimes map
  83. func (conf *Config) GetAllRuntimes() map[string]types.Runtime {
  84. conf.Lock()
  85. rts := conf.Runtimes
  86. conf.Unlock()
  87. return rts
  88. }
  89. // GetExecRoot returns the user configured Exec-root
  90. func (conf *Config) GetExecRoot() string {
  91. return conf.ExecRoot
  92. }
  93. // GetInitPath returns the configured docker-init path
  94. func (conf *Config) GetInitPath() string {
  95. conf.Lock()
  96. defer conf.Unlock()
  97. if conf.InitPath != "" {
  98. return conf.InitPath
  99. }
  100. if conf.DefaultInitBinary != "" {
  101. return conf.DefaultInitBinary
  102. }
  103. return DefaultInitBinary
  104. }
  105. // GetResolvConf returns the appropriate resolv.conf
  106. // Check setupResolvConf on how this is selected
  107. func (conf *Config) GetResolvConf() string {
  108. return conf.ResolvConf
  109. }
  110. // IsSwarmCompatible defines if swarm mode can be enabled in this config
  111. func (conf *Config) IsSwarmCompatible() error {
  112. if conf.LiveRestoreEnabled {
  113. return fmt.Errorf("--live-restore daemon configuration is incompatible with swarm mode")
  114. }
  115. return nil
  116. }
  117. func verifyDefaultIpcMode(mode string) error {
  118. const hint = `use "shareable" or "private"`
  119. dm := container.IpcMode(mode)
  120. if !dm.Valid() {
  121. return fmt.Errorf("default IPC mode setting (%v) is invalid; "+hint, dm)
  122. }
  123. if dm != "" && !dm.IsPrivate() && !dm.IsShareable() {
  124. return fmt.Errorf(`IPC mode "%v" is not supported as default value; `+hint, dm)
  125. }
  126. return nil
  127. }
  128. func verifyDefaultCgroupNsMode(mode string) error {
  129. cm := container.CgroupnsMode(mode)
  130. if !cm.Valid() {
  131. return fmt.Errorf(`default cgroup namespace mode (%v) is invalid; use "host" or "private"`, cm)
  132. }
  133. return nil
  134. }
  135. // ValidatePlatformConfig checks if any platform-specific configuration settings are invalid.
  136. func (conf *Config) ValidatePlatformConfig() error {
  137. if err := verifyDefaultIpcMode(conf.IpcMode); err != nil {
  138. return err
  139. }
  140. return verifyDefaultCgroupNsMode(conf.CgroupNamespaceMode)
  141. }
  142. // IsRootless returns conf.Rootless on Linux but false on Windows
  143. func (conf *Config) IsRootless() bool {
  144. return conf.Rootless
  145. }
  146. func setPlatformDefaults(cfg *Config) error {
  147. cfg.Ulimits = make(map[string]*units.Ulimit)
  148. cfg.ShmSize = opts.MemBytes(DefaultShmSize)
  149. cfg.SeccompProfile = SeccompProfileDefault
  150. cfg.IpcMode = string(DefaultIpcMode)
  151. cfg.Runtimes = make(map[string]types.Runtime)
  152. if cgroups.Mode() != cgroups.Unified {
  153. cfg.CgroupNamespaceMode = string(DefaultCgroupV1NamespaceMode)
  154. } else {
  155. cfg.CgroupNamespaceMode = string(DefaultCgroupNamespaceMode)
  156. }
  157. if rootless.RunningWithRootlessKit() {
  158. cfg.Rootless = true
  159. var err error
  160. // use rootlesskit-docker-proxy for exposing the ports in RootlessKit netns to the initial namespace.
  161. cfg.BridgeConfig.UserlandProxyPath, err = exec.LookPath(rootless.RootlessKitDockerProxyBinary)
  162. if err != nil {
  163. return errors.Wrapf(err, "running with RootlessKit, but %s not installed", rootless.RootlessKitDockerProxyBinary)
  164. }
  165. dataHome, err := homedir.GetDataHome()
  166. if err != nil {
  167. return err
  168. }
  169. runtimeDir, err := homedir.GetRuntimeDir()
  170. if err != nil {
  171. return err
  172. }
  173. cfg.Root = filepath.Join(dataHome, "docker")
  174. cfg.ExecRoot = filepath.Join(runtimeDir, "docker")
  175. cfg.Pidfile = filepath.Join(runtimeDir, "docker.pid")
  176. } else {
  177. cfg.Root = "/var/lib/docker"
  178. cfg.ExecRoot = "/var/run/docker"
  179. cfg.Pidfile = "/var/run/docker.pid"
  180. }
  181. return nil
  182. }