github.com/moby/docker@v26.1.3+incompatible/cmd/dockerd/docker.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/signal"
     7  	"syscall"
     8  
     9  	"github.com/containerd/log"
    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/rootless"
    14  	"github.com/moby/buildkit/util/apicaps"
    15  	"github.com/moby/term"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  var honorXDG bool
    20  
    21  func newDaemonCommand() (*cobra.Command, error) {
    22  	// FIXME(thaJeztah): config.New also looks up default binary-path, but this code is also executed when running "--version".
    23  	cfg, err := config.New()
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	opts := newDaemonOptions(cfg)
    28  
    29  	cmd := &cobra.Command{
    30  		Use:           "dockerd [OPTIONS]",
    31  		Short:         "A self-sufficient runtime for containers.",
    32  		SilenceUsage:  true,
    33  		SilenceErrors: true,
    34  		Args:          NoArgs,
    35  		RunE: func(cmd *cobra.Command, args []string) error {
    36  			opts.flags = cmd.Flags()
    37  			return runDaemon(opts)
    38  		},
    39  		DisableFlagsInUseLine: true,
    40  		Version:               fmt.Sprintf("%s, build %s", dockerversion.Version, dockerversion.GitCommit),
    41  	}
    42  	SetupRootCommand(cmd)
    43  
    44  	flags := cmd.Flags()
    45  	flags.BoolP("version", "v", false, "Print version information and quit")
    46  	defaultDaemonConfigFile, err := getDefaultDaemonConfigFile()
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "Daemon configuration file")
    51  	configureCertsDir()
    52  	opts.installFlags(flags)
    53  	if err := installConfigFlags(opts.daemonConfig, flags); err != nil {
    54  		return nil, err
    55  	}
    56  	installServiceFlags(flags)
    57  
    58  	return cmd, nil
    59  }
    60  
    61  func init() {
    62  	if dockerversion.ProductName != "" {
    63  		apicaps.ExportedProduct = dockerversion.ProductName
    64  	}
    65  	// When running with RootlessKit, $XDG_RUNTIME_DIR, $XDG_DATA_HOME, and $XDG_CONFIG_HOME needs to be
    66  	// honored as the default dirs, because we are unlikely to have permissions to access the system-wide
    67  	// directories.
    68  	//
    69  	// Note that even running with --rootless, when not running with RootlessKit, honorXDG needs to be kept false,
    70  	// because the system-wide directories in the current mount namespace are expected to be accessible.
    71  	// ("rootful" dockerd in rootless dockerd, #38702)
    72  	honorXDG = rootless.RunningWithRootlessKit()
    73  }
    74  
    75  func main() {
    76  	if reexec.Init() {
    77  		return
    78  	}
    79  
    80  	// Ignore SIGPIPE events. These are generated by systemd when journald is restarted while
    81  	// the docker daemon is not restarted and also running under systemd.
    82  	// Fixes https://github.com/docker/docker/issues/19728
    83  	signal.Ignore(syscall.SIGPIPE)
    84  
    85  	// Set terminal emulation based on platform as required.
    86  	_, stdout, stderr := term.StdStreams()
    87  	onError := func(err error) {
    88  		fmt.Fprintf(stderr, "%s\n", err)
    89  		os.Exit(1)
    90  	}
    91  
    92  	// initial log formatting; this setting is updated after the daemon configuration is loaded.
    93  	err := log.SetFormat(log.TextFormat)
    94  	if err != nil {
    95  		onError(err)
    96  	}
    97  
    98  	initLogging(stdout, stderr)
    99  	configureGRPCLog()
   100  
   101  	cmd, err := newDaemonCommand()
   102  	if err != nil {
   103  		onError(err)
   104  	}
   105  	cmd.SetOut(stdout)
   106  	if err := cmd.Execute(); err != nil {
   107  		onError(err)
   108  	}
   109  }