docker.go 3.0 KB

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