docker.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/signal"
  6. "syscall"
  7. "github.com/docker/docker/daemon/config"
  8. "github.com/docker/docker/dockerversion"
  9. "github.com/docker/docker/pkg/jsonmessage"
  10. "github.com/docker/docker/pkg/reexec"
  11. "github.com/docker/docker/pkg/rootless"
  12. "github.com/moby/buildkit/util/apicaps"
  13. "github.com/moby/term"
  14. "github.com/sirupsen/logrus"
  15. "github.com/spf13/cobra"
  16. )
  17. var (
  18. honorXDG bool
  19. )
  20. func newDaemonCommand() (*cobra.Command, error) {
  21. cfg, err := config.New()
  22. if err != nil {
  23. return nil, err
  24. }
  25. opts := newDaemonOptions(cfg)
  26. cmd := &cobra.Command{
  27. Use: "dockerd [OPTIONS]",
  28. Short: "A self-sufficient runtime for containers.",
  29. SilenceUsage: true,
  30. SilenceErrors: true,
  31. Args: NoArgs,
  32. RunE: func(cmd *cobra.Command, args []string) error {
  33. opts.flags = cmd.Flags()
  34. return runDaemon(opts)
  35. },
  36. DisableFlagsInUseLine: true,
  37. Version: fmt.Sprintf("%s, build %s", dockerversion.Version, dockerversion.GitCommit),
  38. }
  39. SetupRootCommand(cmd)
  40. flags := cmd.Flags()
  41. flags.BoolP("version", "v", false, "Print version information and quit")
  42. defaultDaemonConfigFile, err := getDefaultDaemonConfigFile()
  43. if err != nil {
  44. return nil, err
  45. }
  46. flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "Daemon configuration file")
  47. configureCertsDir()
  48. opts.installFlags(flags)
  49. if err := installConfigFlags(opts.daemonConfig, flags); err != nil {
  50. return nil, err
  51. }
  52. installServiceFlags(flags)
  53. return cmd, nil
  54. }
  55. func init() {
  56. if dockerversion.ProductName != "" {
  57. apicaps.ExportedProduct = dockerversion.ProductName
  58. }
  59. // When running with RootlessKit, $XDG_RUNTIME_DIR, $XDG_DATA_HOME, and $XDG_CONFIG_HOME needs to be
  60. // honored as the default dirs, because we are unlikely to have permissions to access the system-wide
  61. // directories.
  62. //
  63. // Note that even running with --rootless, when not running with RootlessKit, honorXDG needs to be kept false,
  64. // because the system-wide directories in the current mount namespace are expected to be accessible.
  65. // ("rootful" dockerd in rootless dockerd, #38702)
  66. honorXDG = rootless.RunningWithRootlessKit()
  67. }
  68. func main() {
  69. if reexec.Init() {
  70. return
  71. }
  72. // Ignore SIGPIPE events. These are generated by systemd when journald is restarted while
  73. // the docker daemon is not restarted and also running under systemd.
  74. // Fixes https://github.com/docker/docker/issues/19728
  75. signal.Ignore(syscall.SIGPIPE)
  76. // initial log formatting; this setting is updated after the daemon configuration is loaded.
  77. logrus.SetFormatter(&logrus.TextFormatter{
  78. TimestampFormat: jsonmessage.RFC3339NanoFixed,
  79. FullTimestamp: true,
  80. })
  81. // Set terminal emulation based on platform as required.
  82. _, stdout, stderr := term.StdStreams()
  83. initLogging(stdout, stderr)
  84. configureGRPCLog()
  85. onError := func(err error) {
  86. fmt.Fprintf(stderr, "%s\n", err)
  87. os.Exit(1)
  88. }
  89. cmd, err := newDaemonCommand()
  90. if err != nil {
  91. onError(err)
  92. }
  93. cmd.SetOut(stdout)
  94. if err := cmd.Execute(); err != nil {
  95. onError(err)
  96. }
  97. }