docker.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/jsonmessage"
  10. "github.com/docker/docker/pkg/reexec"
  11. "github.com/docker/docker/pkg/term"
  12. "github.com/moby/buildkit/util/apicaps"
  13. "github.com/sirupsen/logrus"
  14. "github.com/spf13/cobra"
  15. )
  16. func newDaemonCommand() (*cobra.Command, error) {
  17. opts := newDaemonOptions(config.New())
  18. cmd := &cobra.Command{
  19. Use: "dockerd [OPTIONS]",
  20. Short: "A self-sufficient runtime for containers.",
  21. SilenceUsage: true,
  22. SilenceErrors: true,
  23. Args: cli.NoArgs,
  24. RunE: func(cmd *cobra.Command, args []string) error {
  25. opts.flags = cmd.Flags()
  26. return runDaemon(opts)
  27. },
  28. DisableFlagsInUseLine: true,
  29. Version: fmt.Sprintf("%s, build %s", dockerversion.Version, dockerversion.GitCommit),
  30. }
  31. cli.SetupRootCommand(cmd)
  32. flags := cmd.Flags()
  33. flags.BoolP("version", "v", false, "Print version information and quit")
  34. defaultDaemonConfigFile, err := getDefaultDaemonConfigFile()
  35. if err != nil {
  36. return nil, err
  37. }
  38. flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "Daemon configuration file")
  39. opts.InstallFlags(flags)
  40. if err := installConfigFlags(opts.daemonConfig, flags); err != nil {
  41. return nil, err
  42. }
  43. installServiceFlags(flags)
  44. return cmd, nil
  45. }
  46. func init() {
  47. if dockerversion.ProductName != "" {
  48. apicaps.ExportedProduct = dockerversion.ProductName
  49. }
  50. }
  51. func main() {
  52. if reexec.Init() {
  53. return
  54. }
  55. // initial log formatting; this setting is updated after the daemon configuration is loaded.
  56. logrus.SetFormatter(&logrus.TextFormatter{
  57. TimestampFormat: jsonmessage.RFC3339NanoFixed,
  58. FullTimestamp: true,
  59. })
  60. // Set terminal emulation based on platform as required.
  61. _, stdout, stderr := term.StdStreams()
  62. // @jhowardmsft - maybe there is a historic reason why on non-Windows, stderr is used
  63. // here. However, on Windows it makes no sense and there is no need.
  64. if runtime.GOOS == "windows" {
  65. logrus.SetOutput(stdout)
  66. } else {
  67. logrus.SetOutput(stderr)
  68. }
  69. onError := func(err error) {
  70. fmt.Fprintf(stderr, "%s\n", err)
  71. os.Exit(1)
  72. }
  73. cmd, err := newDaemonCommand()
  74. if err != nil {
  75. onError(err)
  76. }
  77. cmd.SetOutput(stdout)
  78. if err := cmd.Execute(); err != nil {
  79. onError(err)
  80. }
  81. }