diff --git a/cmd/dockerd/builtins/builtins.go b/cmd/dockerd/builtins/builtins.go new file mode 100644 index 0000000000..6c14e39f38 --- /dev/null +++ b/cmd/dockerd/builtins/builtins.go @@ -0,0 +1 @@ +package builtins diff --git a/cmd/dockerd/genwinres_windows.go b/cmd/dockerd/builtins/genwinres_windows.go similarity index 53% rename from cmd/dockerd/genwinres_windows.go rename to cmd/dockerd/builtins/genwinres_windows.go index dd00c662bb..f58997f5cb 100644 --- a/cmd/dockerd/genwinres_windows.go +++ b/cmd/dockerd/builtins/genwinres_windows.go @@ -1,5 +1,5 @@ -//go:generate go-winres make --arch=386,amd64,arm,arm64 --in=../../cli/winresources/dockerd/winres.json --out=../../cli/winresources/dockerd/resource +//go:generate go-winres make --arch=386,amd64,arm,arm64 --in=../../../cli/winresources/dockerd/winres.json --out=../../../cli/winresources/dockerd/resource -package main +package builtins import _ "github.com/docker/docker/cli/winresources/dockerd" diff --git a/cmd/dockerd/main.go b/cmd/dockerd/main.go new file mode 100644 index 0000000000..899ac06b96 --- /dev/null +++ b/cmd/dockerd/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "context" + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/moby/sys/reexec" + "github.com/moby/term" + + "github.com/docker/docker/daemon/command" + + _ "github.com/docker/docker/cmd/dockerd/builtins" +) + +func main() { + if reexec.Init() { + return + } + ctx := context.Background() + + // Ignore SIGPIPE events. These are generated by systemd when journald is restarted while + // the docker daemon is not restarted and also running under systemd. + // Fixes https://github.com/docker/docker/issues/19728 + signal.Ignore(syscall.SIGPIPE) + + // Set terminal emulation based on platform as required. + _, stdout, stderr := term.StdStreams() + onError := func(err error) { + fmt.Fprintf(stderr, "%s\n", err) + os.Exit(1) + } + + r, err := command.NewDaemonRunner(stdout, stderr) + if err != nil { + onError(err) + } + if err := r.Run(ctx); err != nil { + onError(err) + } +} diff --git a/cmd/dockerd/cobra.go b/daemon/command/cobra.go similarity index 99% rename from cmd/dockerd/cobra.go rename to daemon/command/cobra.go index 9d991cdb2d..adc8ce0e16 100644 --- a/cmd/dockerd/cobra.go +++ b/daemon/command/cobra.go @@ -1,4 +1,4 @@ -package main +package command import ( "fmt" diff --git a/cmd/dockerd/config.go b/daemon/command/config.go similarity index 99% rename from cmd/dockerd/config.go rename to daemon/command/config.go index 2fc4983e9c..d9450b42c3 100644 --- a/cmd/dockerd/config.go +++ b/daemon/command/config.go @@ -1,4 +1,4 @@ -package main +package command import ( "runtime" diff --git a/cmd/dockerd/config_unix.go b/daemon/command/config_unix.go similarity index 99% rename from cmd/dockerd/config_unix.go rename to daemon/command/config_unix.go index de3fa443b3..707b7e54d3 100644 --- a/cmd/dockerd/config_unix.go +++ b/daemon/command/config_unix.go @@ -1,6 +1,6 @@ -//go:build linux || freebsd +//go:build unix -package main +package command import ( "net" diff --git a/cmd/dockerd/config_unix_test.go b/daemon/command/config_unix_test.go similarity index 93% rename from cmd/dockerd/config_unix_test.go rename to daemon/command/config_unix_test.go index 5072f3076e..56b6aa1a9c 100644 --- a/cmd/dockerd/config_unix_test.go +++ b/daemon/command/config_unix_test.go @@ -1,6 +1,6 @@ -//go:build linux || freebsd +//go:build unix -package main +package command import ( "testing" diff --git a/cmd/dockerd/config_windows.go b/daemon/command/config_windows.go similarity index 98% rename from cmd/dockerd/config_windows.go rename to daemon/command/config_windows.go index a6af5c7849..85cf45d7fe 100644 --- a/cmd/dockerd/config_windows.go +++ b/daemon/command/config_windows.go @@ -1,4 +1,4 @@ -package main +package command import ( "github.com/docker/docker/daemon/config" diff --git a/cmd/dockerd/daemon.go b/daemon/command/daemon.go similarity index 99% rename from cmd/dockerd/daemon.go rename to daemon/command/daemon.go index 8e475d41f2..ab6f6079c1 100644 --- a/cmd/dockerd/daemon.go +++ b/daemon/command/daemon.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" @@ -39,10 +39,10 @@ import ( buildkit "github.com/docker/docker/builder/builder-next" "github.com/docker/docker/builder/builder-next/exporter" "github.com/docker/docker/builder/dockerfile" - "github.com/docker/docker/cmd/dockerd/debug" - "github.com/docker/docker/cmd/dockerd/trap" "github.com/docker/docker/daemon" "github.com/docker/docker/daemon/cluster" + "github.com/docker/docker/daemon/command/debug" + "github.com/docker/docker/daemon/command/trap" "github.com/docker/docker/daemon/config" "github.com/docker/docker/daemon/listeners" "github.com/docker/docker/dockerversion" diff --git a/cmd/dockerd/daemon_freebsd.go b/daemon/command/daemon_freebsd.go similarity index 96% rename from cmd/dockerd/daemon_freebsd.go rename to daemon/command/daemon_freebsd.go index d1293485a6..c0b920112e 100644 --- a/cmd/dockerd/daemon_freebsd.go +++ b/daemon/command/daemon_freebsd.go @@ -1,4 +1,4 @@ -package main +package command import "github.com/docker/docker/daemon/config" diff --git a/cmd/dockerd/daemon_linux.go b/daemon/command/daemon_linux.go similarity index 99% rename from cmd/dockerd/daemon_linux.go rename to daemon/command/daemon_linux.go index ece18dbb3d..b25a1cfdb2 100644 --- a/cmd/dockerd/daemon_linux.go +++ b/daemon/command/daemon_linux.go @@ -1,4 +1,4 @@ -package main +package command import ( cdcgroups "github.com/containerd/cgroups/v3" diff --git a/cmd/dockerd/daemon_linux_test.go b/daemon/command/daemon_linux_test.go similarity index 99% rename from cmd/dockerd/daemon_linux_test.go rename to daemon/command/daemon_linux_test.go index fb46b48cd3..07f63a5039 100644 --- a/cmd/dockerd/daemon_linux_test.go +++ b/daemon/command/daemon_linux_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "bytes" diff --git a/cmd/dockerd/daemon_test.go b/daemon/command/daemon_test.go similarity index 99% rename from cmd/dockerd/daemon_test.go rename to daemon/command/daemon_test.go index 254ed7d3cf..312a0c1415 100644 --- a/cmd/dockerd/daemon_test.go +++ b/daemon/command/daemon_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "runtime" diff --git a/cmd/dockerd/daemon_unix.go b/daemon/command/daemon_unix.go similarity index 98% rename from cmd/dockerd/daemon_unix.go rename to daemon/command/daemon_unix.go index bfd0dce866..5d6c2384a1 100644 --- a/cmd/dockerd/daemon_unix.go +++ b/daemon/command/daemon_unix.go @@ -1,6 +1,6 @@ -//go:build !windows +//go:build unix -package main +package command import ( "context" diff --git a/cmd/dockerd/daemon_unix_test.go b/daemon/command/daemon_unix_test.go similarity index 98% rename from cmd/dockerd/daemon_unix_test.go rename to daemon/command/daemon_unix_test.go index 1d2abf88c0..eefacb71ef 100644 --- a/cmd/dockerd/daemon_unix_test.go +++ b/daemon/command/daemon_unix_test.go @@ -1,6 +1,6 @@ -//go:build !windows +//go:build unix -package main +package command import ( "testing" diff --git a/cmd/dockerd/daemon_windows.go b/daemon/command/daemon_windows.go similarity index 99% rename from cmd/dockerd/daemon_windows.go rename to daemon/command/daemon_windows.go index e340dbfa2f..7aab0d6b36 100644 --- a/cmd/dockerd/daemon_windows.go +++ b/daemon/command/daemon_windows.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" diff --git a/cmd/dockerd/debug/debug.go b/daemon/command/debug/debug.go similarity index 100% rename from cmd/dockerd/debug/debug.go rename to daemon/command/debug/debug.go diff --git a/cmd/dockerd/debug/debug_test.go b/daemon/command/debug/debug_test.go similarity index 100% rename from cmd/dockerd/debug/debug_test.go rename to daemon/command/debug/debug_test.go diff --git a/cmd/dockerd/docker.go b/daemon/command/docker.go similarity index 76% rename from cmd/dockerd/docker.go rename to daemon/command/docker.go index e75615cb41..15f9863f1d 100644 --- a/cmd/dockerd/docker.go +++ b/daemon/command/docker.go @@ -1,25 +1,21 @@ -package main +package command import ( "context" "fmt" - "os" - "os/signal" - "syscall" + "io" "github.com/containerd/log" "github.com/docker/docker/daemon/config" "github.com/docker/docker/dockerversion" "github.com/docker/docker/pkg/rootless" "github.com/moby/buildkit/util/apicaps" - "github.com/moby/sys/reexec" - "github.com/moby/term" "github.com/spf13/cobra" ) var honorXDG bool -func newDaemonCommand() (*cobra.Command, error) { +func newDaemonCommand(stderr io.Writer) (*cobra.Command, error) { // FIXME(thaJeztah): config.New also looks up default binary-path, but this code is also executed when running "--version". cfg, err := config.New() if err != nil { @@ -42,7 +38,7 @@ func newDaemonCommand() (*cobra.Command, error) { } if opts.Validate { // If config wasn't OK we wouldn't have made it this far. - _, _ = fmt.Fprintln(os.Stderr, "configuration OK") + _, _ = fmt.Fprintln(stderr, "configuration OK") return nil } @@ -100,39 +96,36 @@ func init() { honorXDG = rootless.RunningWithRootlessKit() } -func main() { - if reexec.Init() { - return - } - ctx := context.Background() +// Runner is used to run the daemon command +type Runner interface { + Run(context.Context) error +} - // Ignore SIGPIPE events. These are generated by systemd when journald is restarted while - // the docker daemon is not restarted and also running under systemd. - // Fixes https://github.com/docker/docker/issues/19728 - signal.Ignore(syscall.SIGPIPE) +type daemonRunner struct { + *cobra.Command +} - // Set terminal emulation based on platform as required. - _, stdout, stderr := term.StdStreams() - onError := func(err error) { - fmt.Fprintf(stderr, "%s\n", err) - os.Exit(1) - } +func (d daemonRunner) Run(ctx context.Context) error { + configureGRPCLog(ctx) - // initial log formatting; this setting is updated after the daemon configuration is loaded. + return d.ExecuteContext(ctx) +} + +// NewDaemonRunner creates a new daemon runner with the given +// stdout and stderr writers. +func NewDaemonRunner(stdout, stderr io.Writer) (Runner, error) { err := log.SetFormat(log.TextFormat) if err != nil { - onError(err) + return nil, err } initLogging(stdout, stderr) - configureGRPCLog() - cmd, err := newDaemonCommand() + cmd, err := newDaemonCommand(stderr) if err != nil { - onError(err) + return nil, err } cmd.SetOut(stdout) - if err := cmd.ExecuteContext(ctx); err != nil { - onError(err) - } + + return daemonRunner{cmd}, nil } diff --git a/cmd/dockerd/docker_unix.go b/daemon/command/docker_unix.go similarity index 87% rename from cmd/dockerd/docker_unix.go rename to daemon/command/docker_unix.go index 26b6846850..39602a94e3 100644 --- a/cmd/dockerd/docker_unix.go +++ b/daemon/command/docker_unix.go @@ -1,6 +1,6 @@ -//go:build !windows +//go:build unix -package main +package command import ( "context" diff --git a/cmd/dockerd/docker_windows.go b/daemon/command/docker_windows.go similarity index 98% rename from cmd/dockerd/docker_windows.go rename to daemon/command/docker_windows.go index 1b17b0c36b..b517456bf3 100644 --- a/cmd/dockerd/docker_windows.go +++ b/daemon/command/docker_windows.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" diff --git a/cmd/dockerd/error.go b/daemon/command/error.go similarity index 93% rename from cmd/dockerd/error.go rename to daemon/command/error.go index 33c728fbfa..4bb430b416 100644 --- a/cmd/dockerd/error.go +++ b/daemon/command/error.go @@ -1,4 +1,4 @@ -package main +package command import ( "fmt" diff --git a/cmd/dockerd/grpclog.go b/daemon/command/grpclog.go similarity index 81% rename from cmd/dockerd/grpclog.go rename to daemon/command/grpclog.go index b90408a469..29a94c1ecb 100644 --- a/cmd/dockerd/grpclog.go +++ b/daemon/command/grpclog.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" @@ -13,7 +13,7 @@ import ( // info => trace // warn => debug // error => warn -func configureGRPCLog() { - l := log.G(context.TODO()).WithField("library", "grpc") +func configureGRPCLog(ctx context.Context) { + l := log.G(ctx).WithField("library", "grpc") grpclog.SetLoggerV2(grpclog.NewLoggerV2(l.WriterLevel(log.TraceLevel), l.WriterLevel(log.DebugLevel), l.WriterLevel(log.WarnLevel))) } diff --git a/cmd/dockerd/main_linux_test.go b/daemon/command/main_linux_test.go similarity index 94% rename from cmd/dockerd/main_linux_test.go rename to daemon/command/main_linux_test.go index ddf4c122cf..1db0e19bdf 100644 --- a/cmd/dockerd/main_linux_test.go +++ b/daemon/command/main_linux_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "testing" diff --git a/cmd/dockerd/metrics.go b/daemon/command/metrics.go similarity index 98% rename from cmd/dockerd/metrics.go rename to daemon/command/metrics.go index cff601c4f8..f658cea13a 100644 --- a/cmd/dockerd/metrics.go +++ b/daemon/command/metrics.go @@ -1,4 +1,4 @@ -package main +package command import ( "context" diff --git a/cmd/dockerd/options.go b/daemon/command/options.go similarity index 99% rename from cmd/dockerd/options.go rename to daemon/command/options.go index de745c584f..8c02bac517 100644 --- a/cmd/dockerd/options.go +++ b/daemon/command/options.go @@ -1,4 +1,4 @@ -package main +package command import ( "fmt" diff --git a/cmd/dockerd/options_test.go b/daemon/command/options_test.go similarity index 98% rename from cmd/dockerd/options_test.go rename to daemon/command/options_test.go index 5befad984d..ecde67e17b 100644 --- a/cmd/dockerd/options_test.go +++ b/daemon/command/options_test.go @@ -1,4 +1,4 @@ -package main +package command import ( "path/filepath" diff --git a/cmd/dockerd/required.go b/daemon/command/required.go similarity index 96% rename from cmd/dockerd/required.go rename to daemon/command/required.go index 416c37485f..1b06e75ba5 100644 --- a/cmd/dockerd/required.go +++ b/daemon/command/required.go @@ -1,4 +1,4 @@ -package main +package command import ( "strings" diff --git a/cmd/dockerd/service_unsupported.go b/daemon/command/service_unsupported.go similarity index 87% rename from cmd/dockerd/service_unsupported.go rename to daemon/command/service_unsupported.go index 500f40d46d..148415c048 100644 --- a/cmd/dockerd/service_unsupported.go +++ b/daemon/command/service_unsupported.go @@ -1,6 +1,6 @@ //go:build !windows -package main +package command import ( "github.com/spf13/pflag" diff --git a/cmd/dockerd/service_windows.go b/daemon/command/service_windows.go similarity index 99% rename from cmd/dockerd/service_windows.go rename to daemon/command/service_windows.go index e2fde0774e..9b352c62f0 100644 --- a/cmd/dockerd/service_windows.go +++ b/daemon/command/service_windows.go @@ -1,4 +1,4 @@ -package main +package command import ( "bytes" diff --git a/cmd/dockerd/trap/testfiles/main.go b/daemon/command/trap/testfiles/main.go similarity index 92% rename from cmd/dockerd/trap/testfiles/main.go rename to daemon/command/trap/testfiles/main.go index 40b64bd534..fc5a38277b 100644 --- a/cmd/dockerd/trap/testfiles/main.go +++ b/daemon/command/trap/testfiles/main.go @@ -5,7 +5,7 @@ import ( "syscall" "time" - "github.com/docker/docker/cmd/dockerd/trap" + "github.com/docker/docker/daemon/command/trap" ) func main() { diff --git a/cmd/dockerd/trap/trap.go b/daemon/command/trap/trap.go similarity index 100% rename from cmd/dockerd/trap/trap.go rename to daemon/command/trap/trap.go diff --git a/cmd/dockerd/trap/trap_linux_test.go b/daemon/command/trap/trap_linux_test.go similarity index 100% rename from cmd/dockerd/trap/trap_linux_test.go rename to daemon/command/trap/trap_linux_test.go diff --git a/daemon/info.go b/daemon/info.go index 2035abdc9f..b62fd79044 100644 --- a/daemon/info.go +++ b/daemon/info.go @@ -17,7 +17,7 @@ import ( "github.com/docker/docker/api" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/system" - "github.com/docker/docker/cmd/dockerd/debug" + "github.com/docker/docker/daemon/command/debug" "github.com/docker/docker/daemon/config" "github.com/docker/docker/daemon/internal/filedescriptors" "github.com/docker/docker/daemon/logger"