config_linux.go 5.7 KB

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