docker.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. DisableFlagsInUseLine: true,
  31. }
  32. cli.SetupRootCommand(cmd)
  33. flags := cmd.Flags()
  34. flags.BoolVarP(&opts.version, "version", "v", false, "Print version information and quit")
  35. flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "Daemon configuration file")
  36. opts.InstallFlags(flags)
  37. installConfigFlags(opts.daemonConfig, flags)
  38. installServiceFlags(flags)
  39. return cmd
  40. }
  41. func showVersion() {
  42. fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
  43. }
  44. func main() {
  45. if reexec.Init() {
  46. return
  47. }
  48. // Set terminal emulation based on platform as required.
  49. _, stdout, stderr := term.StdStreams()
  50. // @jhowardmsft - maybe there is a historic reason why on non-Windows, stderr is used
  51. // here. However, on Windows it makes no sense and there is no need.
  52. if runtime.GOOS == "windows" {
  53. logrus.SetOutput(stdout)
  54. } else {
  55. logrus.SetOutput(stderr)
  56. }
  57. cmd := newDaemonCommand()
  58. cmd.SetOutput(stdout)
  59. if err := cmd.Execute(); err != nil {
  60. fmt.Fprintf(stderr, "%s\n", err)
  61. os.Exit(1)
  62. }
  63. }