config_unix.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // +build linux freebsd
  2. package daemon
  3. import (
  4. "fmt"
  5. "net"
  6. "github.com/docker/docker/opts"
  7. flag "github.com/docker/docker/pkg/mflag"
  8. runconfigopts "github.com/docker/docker/runconfig/opts"
  9. "github.com/docker/engine-api/types"
  10. "github.com/docker/go-units"
  11. )
  12. var (
  13. defaultPidFile = "/var/run/docker.pid"
  14. defaultGraph = "/var/lib/docker"
  15. defaultExecRoot = "/var/run/docker"
  16. )
  17. // Config defines the configuration of a docker daemon.
  18. // It includes json tags to deserialize configuration from a file
  19. // using the same names that the flags in the command line uses.
  20. type Config struct {
  21. CommonConfig
  22. // Fields below here are platform specific.
  23. CgroupParent string `json:"cgroup-parent,omitempty"`
  24. ContainerdAddr string `json:"containerd,omitempty"`
  25. EnableSelinuxSupport bool `json:"selinux-enabled,omitempty"`
  26. ExecRoot string `json:"exec-root,omitempty"`
  27. RemappedRoot string `json:"userns-remap,omitempty"`
  28. Ulimits map[string]*units.Ulimit `json:"default-ulimits,omitempty"`
  29. Runtimes map[string]types.Runtime `json:"runtimes,omitempty"`
  30. DefaultRuntime string `json:"default-runtime,omitempty"`
  31. }
  32. // bridgeConfig stores all the bridge driver specific
  33. // configuration.
  34. type bridgeConfig struct {
  35. commonBridgeConfig
  36. // Fields below here are platform specific.
  37. EnableIPv6 bool `json:"ipv6,omitempty"`
  38. EnableIPTables bool `json:"iptables,omitempty"`
  39. EnableIPForward bool `json:"ip-forward,omitempty"`
  40. EnableIPMasq bool `json:"ip-masq,omitempty"`
  41. EnableUserlandProxy bool `json:"userland-proxy,omitempty"`
  42. DefaultIP net.IP `json:"ip,omitempty"`
  43. IP string `json:"bip,omitempty"`
  44. FixedCIDRv6 string `json:"fixed-cidr-v6,omitempty"`
  45. DefaultGatewayIPv4 net.IP `json:"default-gateway,omitempty"`
  46. DefaultGatewayIPv6 net.IP `json:"default-gateway-v6,omitempty"`
  47. InterContainerCommunication bool `json:"icc,omitempty"`
  48. }
  49. // InstallFlags adds command-line options to the top-level flag parser for
  50. // the current process.
  51. // Subsequent calls to `flag.Parse` will populate config with values parsed
  52. // from the command-line.
  53. func (config *Config) InstallFlags(cmd *flag.FlagSet, usageFn func(string) string) {
  54. // First handle install flags which are consistent cross-platform
  55. config.InstallCommonFlags(cmd, usageFn)
  56. // Then platform-specific install flags
  57. cmd.BoolVar(&config.EnableSelinuxSupport, []string{"-selinux-enabled"}, false, usageFn("Enable selinux support"))
  58. cmd.StringVar(&config.SocketGroup, []string{"G", "-group"}, "docker", usageFn("Group for the unix socket"))
  59. config.Ulimits = make(map[string]*units.Ulimit)
  60. cmd.Var(runconfigopts.NewUlimitOpt(&config.Ulimits), []string{"-default-ulimit"}, usageFn("Set default ulimits for containers"))
  61. cmd.BoolVar(&config.bridgeConfig.EnableIPTables, []string{"#iptables", "-iptables"}, true, usageFn("Enable addition of iptables rules"))
  62. cmd.BoolVar(&config.bridgeConfig.EnableIPForward, []string{"#ip-forward", "-ip-forward"}, true, usageFn("Enable net.ipv4.ip_forward"))
  63. cmd.BoolVar(&config.bridgeConfig.EnableIPMasq, []string{"-ip-masq"}, true, usageFn("Enable IP masquerading"))
  64. cmd.BoolVar(&config.bridgeConfig.EnableIPv6, []string{"-ipv6"}, false, usageFn("Enable IPv6 networking"))
  65. cmd.StringVar(&config.ExecRoot, []string{"-exec-root"}, defaultExecRoot, usageFn("Root directory for execution state files"))
  66. cmd.StringVar(&config.bridgeConfig.IP, []string{"#bip", "-bip"}, "", usageFn("Specify network bridge IP"))
  67. cmd.StringVar(&config.bridgeConfig.Iface, []string{"b", "-bridge"}, "", usageFn("Attach containers to a network bridge"))
  68. cmd.StringVar(&config.bridgeConfig.FixedCIDR, []string{"-fixed-cidr"}, "", usageFn("IPv4 subnet for fixed IPs"))
  69. cmd.StringVar(&config.bridgeConfig.FixedCIDRv6, []string{"-fixed-cidr-v6"}, "", usageFn("IPv6 subnet for fixed IPs"))
  70. cmd.Var(opts.NewIPOpt(&config.bridgeConfig.DefaultGatewayIPv4, ""), []string{"-default-gateway"}, usageFn("Container default gateway IPv4 address"))
  71. cmd.Var(opts.NewIPOpt(&config.bridgeConfig.DefaultGatewayIPv6, ""), []string{"-default-gateway-v6"}, usageFn("Container default gateway IPv6 address"))
  72. cmd.BoolVar(&config.bridgeConfig.InterContainerCommunication, []string{"#icc", "-icc"}, true, usageFn("Enable inter-container communication"))
  73. cmd.Var(opts.NewIPOpt(&config.bridgeConfig.DefaultIP, "0.0.0.0"), []string{"#ip", "-ip"}, usageFn("Default IP when binding container ports"))
  74. cmd.BoolVar(&config.bridgeConfig.EnableUserlandProxy, []string{"-userland-proxy"}, true, usageFn("Use userland proxy for loopback traffic"))
  75. cmd.BoolVar(&config.EnableCors, []string{"#api-enable-cors", "#-api-enable-cors"}, false, usageFn("Enable CORS headers in the remote API, this is deprecated by --api-cors-header"))
  76. cmd.StringVar(&config.CgroupParent, []string{"-cgroup-parent"}, "", usageFn("Set parent cgroup for all containers"))
  77. cmd.StringVar(&config.RemappedRoot, []string{"-userns-remap"}, "", usageFn("User/Group setting for user namespaces"))
  78. cmd.StringVar(&config.ContainerdAddr, []string{"-containerd"}, "", usageFn("Path to containerd socket"))
  79. cmd.BoolVar(&config.LiveRestore, []string{"-live-restore"}, false, usageFn("Enable live restore of docker when containers are still running"))
  80. config.Runtimes = make(map[string]types.Runtime)
  81. cmd.Var(runconfigopts.NewNamedRuntimeOpt("runtimes", &config.Runtimes, stockRuntimeName), []string{"-add-runtime"}, usageFn("Register an additional OCI compatible runtime"))
  82. cmd.StringVar(&config.DefaultRuntime, []string{"-default-runtime"}, stockRuntimeName, usageFn("Default OCI runtime to be used"))
  83. config.attachExperimentalFlags(cmd, usageFn)
  84. }
  85. // GetRuntime returns the runtime path and arguments for a given
  86. // runtime name
  87. func (config *Config) GetRuntime(name string) *types.Runtime {
  88. config.reloadLock.Lock()
  89. defer config.reloadLock.Unlock()
  90. if rt, ok := config.Runtimes[name]; ok {
  91. return &rt
  92. }
  93. return nil
  94. }
  95. // GetDefaultRuntimeName returns the current default runtime
  96. func (config *Config) GetDefaultRuntimeName() string {
  97. config.reloadLock.Lock()
  98. rt := config.DefaultRuntime
  99. config.reloadLock.Unlock()
  100. return rt
  101. }
  102. // GetAllRuntimes returns a copy of the runtimes map
  103. func (config *Config) GetAllRuntimes() map[string]types.Runtime {
  104. config.reloadLock.Lock()
  105. rts := config.Runtimes
  106. config.reloadLock.Unlock()
  107. return rts
  108. }
  109. func (config *Config) isSwarmCompatible() error {
  110. if config.IsValueSet("cluster-store") || config.IsValueSet("cluster-advertise") {
  111. return fmt.Errorf("--cluster-store and --cluster-advertise daemon configurations are incompatible with swarm mode")
  112. }
  113. if config.LiveRestore {
  114. return fmt.Errorf("--live-restore daemon configuration is incompatible with swarm mode")
  115. }
  116. return nil
  117. }