info.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package system
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "time"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/swarm"
  9. "github.com/docker/docker/cli"
  10. "github.com/docker/docker/cli/command"
  11. "github.com/docker/docker/cli/debug"
  12. "github.com/docker/docker/pkg/ioutils"
  13. "github.com/docker/docker/pkg/templates"
  14. "github.com/docker/go-units"
  15. "github.com/spf13/cobra"
  16. "golang.org/x/net/context"
  17. )
  18. type infoOptions struct {
  19. format string
  20. }
  21. // NewInfoCommand creates a new cobra.Command for `docker info`
  22. func NewInfoCommand(dockerCli *command.DockerCli) *cobra.Command {
  23. var opts infoOptions
  24. cmd := &cobra.Command{
  25. Use: "info [OPTIONS]",
  26. Short: "Display system-wide information",
  27. Args: cli.NoArgs,
  28. RunE: func(cmd *cobra.Command, args []string) error {
  29. return runInfo(dockerCli, &opts)
  30. },
  31. }
  32. flags := cmd.Flags()
  33. flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
  34. return cmd
  35. }
  36. func runInfo(dockerCli *command.DockerCli, opts *infoOptions) error {
  37. ctx := context.Background()
  38. info, err := dockerCli.Client().Info(ctx)
  39. if err != nil {
  40. return err
  41. }
  42. if opts.format == "" {
  43. return prettyPrintInfo(dockerCli, info)
  44. }
  45. return formatInfo(dockerCli, info, opts.format)
  46. }
  47. func prettyPrintInfo(dockerCli *command.DockerCli, info types.Info) error {
  48. fmt.Fprintf(dockerCli.Out(), "Containers: %d\n", info.Containers)
  49. fmt.Fprintf(dockerCli.Out(), " Running: %d\n", info.ContainersRunning)
  50. fmt.Fprintf(dockerCli.Out(), " Paused: %d\n", info.ContainersPaused)
  51. fmt.Fprintf(dockerCli.Out(), " Stopped: %d\n", info.ContainersStopped)
  52. fmt.Fprintf(dockerCli.Out(), "Images: %d\n", info.Images)
  53. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Server Version: %s\n", info.ServerVersion)
  54. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Storage Driver: %s\n", info.Driver)
  55. if info.DriverStatus != nil {
  56. for _, pair := range info.DriverStatus {
  57. fmt.Fprintf(dockerCli.Out(), " %s: %s\n", pair[0], pair[1])
  58. }
  59. }
  60. if info.SystemStatus != nil {
  61. for _, pair := range info.SystemStatus {
  62. fmt.Fprintf(dockerCli.Out(), "%s: %s\n", pair[0], pair[1])
  63. }
  64. }
  65. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Logging Driver: %s\n", info.LoggingDriver)
  66. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Cgroup Driver: %s\n", info.CgroupDriver)
  67. fmt.Fprintf(dockerCli.Out(), "Plugins: \n")
  68. fmt.Fprintf(dockerCli.Out(), " Volume:")
  69. fmt.Fprintf(dockerCli.Out(), " %s", strings.Join(info.Plugins.Volume, " "))
  70. fmt.Fprintf(dockerCli.Out(), "\n")
  71. fmt.Fprintf(dockerCli.Out(), " Network:")
  72. fmt.Fprintf(dockerCli.Out(), " %s", strings.Join(info.Plugins.Network, " "))
  73. fmt.Fprintf(dockerCli.Out(), "\n")
  74. if len(info.Plugins.Authorization) != 0 {
  75. fmt.Fprintf(dockerCli.Out(), " Authorization:")
  76. fmt.Fprintf(dockerCli.Out(), " %s", strings.Join(info.Plugins.Authorization, " "))
  77. fmt.Fprintf(dockerCli.Out(), "\n")
  78. }
  79. fmt.Fprintf(dockerCli.Out(), "Swarm: %v\n", info.Swarm.LocalNodeState)
  80. if info.Swarm.LocalNodeState != swarm.LocalNodeStateInactive && info.Swarm.LocalNodeState != swarm.LocalNodeStateLocked {
  81. fmt.Fprintf(dockerCli.Out(), " NodeID: %s\n", info.Swarm.NodeID)
  82. if info.Swarm.Error != "" {
  83. fmt.Fprintf(dockerCli.Out(), " Error: %v\n", info.Swarm.Error)
  84. }
  85. fmt.Fprintf(dockerCli.Out(), " Is Manager: %v\n", info.Swarm.ControlAvailable)
  86. if info.Swarm.ControlAvailable && info.Swarm.Error == "" && info.Swarm.LocalNodeState != swarm.LocalNodeStateError {
  87. fmt.Fprintf(dockerCli.Out(), " ClusterID: %s\n", info.Swarm.Cluster.ID)
  88. fmt.Fprintf(dockerCli.Out(), " Managers: %d\n", info.Swarm.Managers)
  89. fmt.Fprintf(dockerCli.Out(), " Nodes: %d\n", info.Swarm.Nodes)
  90. fmt.Fprintf(dockerCli.Out(), " Orchestration:\n")
  91. taskHistoryRetentionLimit := int64(0)
  92. if info.Swarm.Cluster.Spec.Orchestration.TaskHistoryRetentionLimit != nil {
  93. taskHistoryRetentionLimit = *info.Swarm.Cluster.Spec.Orchestration.TaskHistoryRetentionLimit
  94. }
  95. fmt.Fprintf(dockerCli.Out(), " Task History Retention Limit: %d\n", taskHistoryRetentionLimit)
  96. fmt.Fprintf(dockerCli.Out(), " Raft:\n")
  97. fmt.Fprintf(dockerCli.Out(), " Snapshot Interval: %d\n", info.Swarm.Cluster.Spec.Raft.SnapshotInterval)
  98. if info.Swarm.Cluster.Spec.Raft.KeepOldSnapshots != nil {
  99. fmt.Fprintf(dockerCli.Out(), " Number of Old Snapshots to Retain: %d\n", *info.Swarm.Cluster.Spec.Raft.KeepOldSnapshots)
  100. }
  101. fmt.Fprintf(dockerCli.Out(), " Heartbeat Tick: %d\n", info.Swarm.Cluster.Spec.Raft.HeartbeatTick)
  102. fmt.Fprintf(dockerCli.Out(), " Election Tick: %d\n", info.Swarm.Cluster.Spec.Raft.ElectionTick)
  103. fmt.Fprintf(dockerCli.Out(), " Dispatcher:\n")
  104. fmt.Fprintf(dockerCli.Out(), " Heartbeat Period: %s\n", units.HumanDuration(time.Duration(info.Swarm.Cluster.Spec.Dispatcher.HeartbeatPeriod)))
  105. fmt.Fprintf(dockerCli.Out(), " CA Configuration:\n")
  106. fmt.Fprintf(dockerCli.Out(), " Expiry Duration: %s\n", units.HumanDuration(info.Swarm.Cluster.Spec.CAConfig.NodeCertExpiry))
  107. if len(info.Swarm.Cluster.Spec.CAConfig.ExternalCAs) > 0 {
  108. fmt.Fprintf(dockerCli.Out(), " External CAs:\n")
  109. for _, entry := range info.Swarm.Cluster.Spec.CAConfig.ExternalCAs {
  110. fmt.Fprintf(dockerCli.Out(), " %s: %s\n", entry.Protocol, entry.URL)
  111. }
  112. }
  113. }
  114. fmt.Fprintf(dockerCli.Out(), " Node Address: %s\n", info.Swarm.NodeAddr)
  115. managers := []string{}
  116. for _, entry := range info.Swarm.RemoteManagers {
  117. managers = append(managers, entry.Addr)
  118. }
  119. if len(managers) > 0 {
  120. sort.Strings(managers)
  121. fmt.Fprintf(dockerCli.Out(), " Manager Addresses:\n")
  122. for _, entry := range managers {
  123. fmt.Fprintf(dockerCli.Out(), " %s\n", entry)
  124. }
  125. }
  126. }
  127. if len(info.Runtimes) > 0 {
  128. fmt.Fprintf(dockerCli.Out(), "Runtimes:")
  129. for name := range info.Runtimes {
  130. fmt.Fprintf(dockerCli.Out(), " %s", name)
  131. }
  132. fmt.Fprint(dockerCli.Out(), "\n")
  133. fmt.Fprintf(dockerCli.Out(), "Default Runtime: %s\n", info.DefaultRuntime)
  134. }
  135. if info.OSType == "linux" {
  136. fmt.Fprintf(dockerCli.Out(), "Init Binary: %v\n", info.InitBinary)
  137. for _, ci := range []struct {
  138. Name string
  139. Commit types.Commit
  140. }{
  141. {"containerd", info.ContainerdCommit},
  142. {"runc", info.RuncCommit},
  143. {"init", info.InitCommit},
  144. } {
  145. fmt.Fprintf(dockerCli.Out(), "%s version: %s", ci.Name, ci.Commit.ID)
  146. if ci.Commit.ID != ci.Commit.Expected {
  147. fmt.Fprintf(dockerCli.Out(), " (expected: %s)", ci.Commit.Expected)
  148. }
  149. fmt.Fprintf(dockerCli.Out(), "\n")
  150. }
  151. if len(info.SecurityOptions) != 0 {
  152. kvs, err := types.DecodeSecurityOptions(info.SecurityOptions)
  153. if err != nil {
  154. return err
  155. }
  156. fmt.Fprintf(dockerCli.Out(), "Security Options:\n")
  157. for _, so := range kvs {
  158. fmt.Fprintf(dockerCli.Out(), " %s\n", so.Name)
  159. for _, o := range so.Options {
  160. switch o.Key {
  161. case "profile":
  162. if o.Value != "default" {
  163. fmt.Fprintf(dockerCli.Err(), " WARNING: You're not using the default seccomp profile\n")
  164. }
  165. fmt.Fprintf(dockerCli.Out(), " Profile: %s\n", o.Value)
  166. }
  167. }
  168. }
  169. }
  170. }
  171. // Isolation only has meaning on a Windows daemon.
  172. if info.OSType == "windows" {
  173. fmt.Fprintf(dockerCli.Out(), "Default Isolation: %v\n", info.Isolation)
  174. }
  175. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Kernel Version: %s\n", info.KernelVersion)
  176. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Operating System: %s\n", info.OperatingSystem)
  177. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "OSType: %s\n", info.OSType)
  178. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Architecture: %s\n", info.Architecture)
  179. fmt.Fprintf(dockerCli.Out(), "CPUs: %d\n", info.NCPU)
  180. fmt.Fprintf(dockerCli.Out(), "Total Memory: %s\n", units.BytesSize(float64(info.MemTotal)))
  181. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Name: %s\n", info.Name)
  182. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "ID: %s\n", info.ID)
  183. fmt.Fprintf(dockerCli.Out(), "Docker Root Dir: %s\n", info.DockerRootDir)
  184. fmt.Fprintf(dockerCli.Out(), "Debug Mode (client): %v\n", debug.IsEnabled())
  185. fmt.Fprintf(dockerCli.Out(), "Debug Mode (server): %v\n", info.Debug)
  186. if info.Debug {
  187. fmt.Fprintf(dockerCli.Out(), " File Descriptors: %d\n", info.NFd)
  188. fmt.Fprintf(dockerCli.Out(), " Goroutines: %d\n", info.NGoroutines)
  189. fmt.Fprintf(dockerCli.Out(), " System Time: %s\n", info.SystemTime)
  190. fmt.Fprintf(dockerCli.Out(), " EventsListeners: %d\n", info.NEventsListener)
  191. }
  192. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Http Proxy: %s\n", info.HTTPProxy)
  193. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Https Proxy: %s\n", info.HTTPSProxy)
  194. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "No Proxy: %s\n", info.NoProxy)
  195. if info.IndexServerAddress != "" {
  196. u := dockerCli.ConfigFile().AuthConfigs[info.IndexServerAddress].Username
  197. if len(u) > 0 {
  198. fmt.Fprintf(dockerCli.Out(), "Username: %v\n", u)
  199. }
  200. fmt.Fprintf(dockerCli.Out(), "Registry: %v\n", info.IndexServerAddress)
  201. }
  202. if info.Labels != nil {
  203. fmt.Fprintln(dockerCli.Out(), "Labels:")
  204. for _, attribute := range info.Labels {
  205. fmt.Fprintf(dockerCli.Out(), " %s\n", attribute)
  206. }
  207. // TODO: Engine labels with duplicate keys has been deprecated in 1.13 and will be error out
  208. // after 3 release cycles (1.16). For now, a WARNING will be generated. The following will
  209. // be removed eventually.
  210. labelMap := map[string]string{}
  211. for _, label := range info.Labels {
  212. stringSlice := strings.SplitN(label, "=", 2)
  213. if len(stringSlice) > 1 {
  214. // If there is a conflict we will throw out a warning
  215. if v, ok := labelMap[stringSlice[0]]; ok && v != stringSlice[1] {
  216. fmt.Fprintln(dockerCli.Err(), "WARNING: labels with duplicate keys and conflicting values have been deprecated")
  217. break
  218. }
  219. labelMap[stringSlice[0]] = stringSlice[1]
  220. }
  221. }
  222. }
  223. fmt.Fprintf(dockerCli.Out(), "Experimental: %v\n", info.ExperimentalBuild)
  224. if info.ClusterStore != "" {
  225. fmt.Fprintf(dockerCli.Out(), "Cluster Store: %s\n", info.ClusterStore)
  226. }
  227. if info.ClusterAdvertise != "" {
  228. fmt.Fprintf(dockerCli.Out(), "Cluster Advertise: %s\n", info.ClusterAdvertise)
  229. }
  230. if info.RegistryConfig != nil && (len(info.RegistryConfig.InsecureRegistryCIDRs) > 0 || len(info.RegistryConfig.IndexConfigs) > 0) {
  231. fmt.Fprintln(dockerCli.Out(), "Insecure Registries:")
  232. for _, registry := range info.RegistryConfig.IndexConfigs {
  233. if registry.Secure == false {
  234. fmt.Fprintf(dockerCli.Out(), " %s\n", registry.Name)
  235. }
  236. }
  237. for _, registry := range info.RegistryConfig.InsecureRegistryCIDRs {
  238. mask, _ := registry.Mask.Size()
  239. fmt.Fprintf(dockerCli.Out(), " %s/%d\n", registry.IP.String(), mask)
  240. }
  241. }
  242. if info.RegistryConfig != nil && len(info.RegistryConfig.Mirrors) > 0 {
  243. fmt.Fprintln(dockerCli.Out(), "Registry Mirrors:")
  244. for _, mirror := range info.RegistryConfig.Mirrors {
  245. fmt.Fprintf(dockerCli.Out(), " %s\n", mirror)
  246. }
  247. }
  248. fmt.Fprintf(dockerCli.Out(), "Live Restore Enabled: %v\n\n", info.LiveRestoreEnabled)
  249. // Only output these warnings if the server does not support these features
  250. if info.OSType != "windows" {
  251. printStorageDriverWarnings(dockerCli, info)
  252. if !info.MemoryLimit {
  253. fmt.Fprintln(dockerCli.Err(), "WARNING: No memory limit support")
  254. }
  255. if !info.SwapLimit {
  256. fmt.Fprintln(dockerCli.Err(), "WARNING: No swap limit support")
  257. }
  258. if !info.KernelMemory {
  259. fmt.Fprintln(dockerCli.Err(), "WARNING: No kernel memory limit support")
  260. }
  261. if !info.OomKillDisable {
  262. fmt.Fprintln(dockerCli.Err(), "WARNING: No oom kill disable support")
  263. }
  264. if !info.CPUCfsQuota {
  265. fmt.Fprintln(dockerCli.Err(), "WARNING: No cpu cfs quota support")
  266. }
  267. if !info.CPUCfsPeriod {
  268. fmt.Fprintln(dockerCli.Err(), "WARNING: No cpu cfs period support")
  269. }
  270. if !info.CPUShares {
  271. fmt.Fprintln(dockerCli.Err(), "WARNING: No cpu shares support")
  272. }
  273. if !info.CPUSet {
  274. fmt.Fprintln(dockerCli.Err(), "WARNING: No cpuset support")
  275. }
  276. if !info.IPv4Forwarding {
  277. fmt.Fprintln(dockerCli.Err(), "WARNING: IPv4 forwarding is disabled")
  278. }
  279. if !info.BridgeNfIptables {
  280. fmt.Fprintln(dockerCli.Err(), "WARNING: bridge-nf-call-iptables is disabled")
  281. }
  282. if !info.BridgeNfIP6tables {
  283. fmt.Fprintln(dockerCli.Err(), "WARNING: bridge-nf-call-ip6tables is disabled")
  284. }
  285. }
  286. return nil
  287. }
  288. func printStorageDriverWarnings(dockerCli *command.DockerCli, info types.Info) {
  289. if info.DriverStatus == nil {
  290. return
  291. }
  292. for _, pair := range info.DriverStatus {
  293. if pair[0] == "Data loop file" {
  294. fmt.Fprintf(dockerCli.Err(), "WARNING: %s: usage of loopback devices is strongly discouraged for production use.\n Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.\n", info.Driver)
  295. }
  296. if pair[0] == "Supports d_type" && pair[1] == "false" {
  297. backingFs := getBackingFs(info)
  298. msg := fmt.Sprintf("WARNING: %s: the backing %s filesystem is formatted without d_type support, which leads to incorrect behavior.\n", info.Driver, backingFs)
  299. if backingFs == "xfs" {
  300. msg += " Reformat the filesystem with ftype=1 to enable d_type support.\n"
  301. }
  302. msg += " Running without d_type support will not be supported in future releases."
  303. fmt.Fprintln(dockerCli.Err(), msg)
  304. }
  305. }
  306. }
  307. func getBackingFs(info types.Info) string {
  308. if info.DriverStatus == nil {
  309. return ""
  310. }
  311. for _, pair := range info.DriverStatus {
  312. if pair[0] == "Backing Filesystem" {
  313. return pair[1]
  314. }
  315. }
  316. return ""
  317. }
  318. func formatInfo(dockerCli *command.DockerCli, info types.Info, format string) error {
  319. tmpl, err := templates.Parse(format)
  320. if err != nil {
  321. return cli.StatusError{StatusCode: 64,
  322. Status: "Template parsing error: " + err.Error()}
  323. }
  324. err = tmpl.Execute(dockerCli.Out(), info)
  325. dockerCli.Out().Write([]byte{'\n'})
  326. return err
  327. }