github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/cmd/dockerd/docker.go (about)

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