docker.go 2.9 KB

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