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