docker.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "github.com/docker/docker/cli"
  7. "github.com/docker/docker/daemon/config"
  8. "github.com/docker/docker/dockerversion"
  9. "github.com/docker/docker/pkg/reexec"
  10. "github.com/docker/docker/pkg/term"
  11. "github.com/sirupsen/logrus"
  12. "github.com/spf13/cobra"
  13. )
  14. func newDaemonCommand() *cobra.Command {
  15. opts := newDaemonOptions(config.New())
  16. cmd := &cobra.Command{
  17. Use: "dockerd [OPTIONS]",
  18. Short: "A self-sufficient runtime for containers.",
  19. SilenceUsage: true,
  20. SilenceErrors: true,
  21. Args: cli.NoArgs,
  22. RunE: func(cmd *cobra.Command, args []string) error {
  23. if opts.version {
  24. showVersion()
  25. return nil
  26. }
  27. opts.flags = cmd.Flags()
  28. return runDaemon(opts)
  29. },
  30. }
  31. cli.SetupRootCommand(cmd)
  32. flags := cmd.Flags()
  33. flags.BoolVarP(&opts.version, "version", "v", false, "Print version information and quit")
  34. flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "Daemon configuration file")
  35. opts.InstallFlags(flags)
  36. installConfigFlags(opts.daemonConfig, flags)
  37. installServiceFlags(flags)
  38. return cmd
  39. }
  40. func showVersion() {
  41. fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
  42. }
  43. func main() {
  44. if reexec.Init() {
  45. return
  46. }
  47. // Set terminal emulation based on platform as required.
  48. _, stdout, stderr := term.StdStreams()
  49. // @jhowardmsft - maybe there is a historic reason why on non-Windows, stderr is used
  50. // here. However, on Windows it makes no sense and there is no need.
  51. if runtime.GOOS == "windows" {
  52. logrus.SetOutput(stdout)
  53. } else {
  54. logrus.SetOutput(stderr)
  55. }
  56. cmd := newDaemonCommand()
  57. cmd.SetOutput(stdout)
  58. if err := cmd.Execute(); err != nil {
  59. fmt.Fprintf(stderr, "%s\n", err)
  60. os.Exit(1)
  61. }
  62. }