docker.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/docker/docker/cli"
  9. "github.com/docker/docker/daemon/config"
  10. "github.com/docker/docker/dockerversion"
  11. "github.com/docker/docker/pkg/reexec"
  12. "github.com/docker/docker/pkg/term"
  13. "github.com/spf13/cobra"
  14. )
  15. func newDaemonCommand() *cobra.Command {
  16. opts := newDaemonOptions(config.New())
  17. cmd := &cobra.Command{
  18. Use: "dockerd [OPTIONS]",
  19. Short: "A self-sufficient runtime for containers.",
  20. SilenceUsage: true,
  21. SilenceErrors: true,
  22. Args: cli.NoArgs,
  23. RunE: func(cmd *cobra.Command, args []string) error {
  24. opts.flags = cmd.Flags()
  25. return runDaemon(opts)
  26. },
  27. }
  28. cli.SetupRootCommand(cmd)
  29. flags := cmd.Flags()
  30. flags.BoolVarP(&opts.version, "version", "v", false, "Print version information and quit")
  31. flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "Daemon configuration file")
  32. opts.InstallFlags(flags)
  33. installConfigFlags(opts.daemonConfig, flags)
  34. installServiceFlags(flags)
  35. return cmd
  36. }
  37. func runDaemon(opts *daemonOptions) error {
  38. if opts.version {
  39. showVersion()
  40. return nil
  41. }
  42. daemonCli := NewDaemonCli()
  43. // Windows specific settings as these are not defaulted.
  44. if runtime.GOOS == "windows" {
  45. if opts.daemonConfig.Pidfile == "" {
  46. opts.daemonConfig.Pidfile = filepath.Join(opts.daemonConfig.Root, "docker.pid")
  47. }
  48. if opts.configFile == "" {
  49. opts.configFile = filepath.Join(opts.daemonConfig.Root, `config\daemon.json`)
  50. }
  51. }
  52. // On Windows, this may be launching as a service or with an option to
  53. // register the service.
  54. stop, runAsService, err := initService(daemonCli)
  55. if err != nil {
  56. logrus.Fatal(err)
  57. }
  58. if stop {
  59. return nil
  60. }
  61. // If Windows SCM manages the service - no need for PID files
  62. if runAsService {
  63. opts.daemonConfig.Pidfile = ""
  64. }
  65. err = daemonCli.start(opts)
  66. notifyShutdown(err)
  67. return err
  68. }
  69. func showVersion() {
  70. fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
  71. }
  72. func main() {
  73. if reexec.Init() {
  74. return
  75. }
  76. // Set terminal emulation based on platform as required.
  77. _, stdout, stderr := term.StdStreams()
  78. // @jhowardmsft - maybe there is a historic reason why on non-Windows, stderr is used
  79. // here. However, on Windows it makes no sense and there is no need.
  80. if runtime.GOOS == "windows" {
  81. logrus.SetOutput(stdout)
  82. } else {
  83. logrus.SetOutput(stderr)
  84. }
  85. cmd := newDaemonCommand()
  86. cmd.SetOutput(stdout)
  87. if err := cmd.Execute(); err != nil {
  88. fmt.Fprintf(stderr, "%s\n", err)
  89. os.Exit(1)
  90. }
  91. }