2013-01-19 16:07:19 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-04-10 12:24:15 +02:00
|
|
|
"fmt"
|
2016-08-03 12:20:46 -04:00
|
|
|
"os"
|
2016-10-17 11:48:51 -07:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2014-02-01 03:38:39 -08:00
|
|
|
|
2015-03-26 23:22:04 +01:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-06-21 16:42:47 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2017-01-23 12:23:07 +01:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2015-11-09 19:32:46 +01:00
|
|
|
"github.com/docker/docker/dockerversion"
|
2014-10-30 14:48:30 +02:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-01-23 17:33:49 -08:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2016-06-21 16:42:47 -04:00
|
|
|
"github.com/spf13/cobra"
|
2013-01-23 23:14:46 -08:00
|
|
|
)
|
|
|
|
|
2016-06-21 16:42:47 -04:00
|
|
|
func newDaemonCommand() *cobra.Command {
|
2017-06-01 13:34:31 -07:00
|
|
|
opts := newDaemonOptions(config.New())
|
2015-03-19 18:07:56 -07:00
|
|
|
|
2016-06-21 16:42:47 -04:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "dockerd [OPTIONS]",
|
|
|
|
Short: "A self-sufficient runtime for containers.",
|
|
|
|
SilenceUsage: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.flags = cmd.Flags()
|
|
|
|
return runDaemon(opts)
|
|
|
|
},
|
2015-05-07 09:49:07 -07:00
|
|
|
}
|
2016-06-23 11:25:51 -04:00
|
|
|
cli.SetupRootCommand(cmd)
|
2015-05-07 09:49:07 -07:00
|
|
|
|
2016-06-21 16:42:47 -04:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.version, "version", "v", false, "Print version information and quit")
|
2017-03-30 12:01:00 +02:00
|
|
|
flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "Daemon configuration file")
|
2017-06-01 13:34:31 -07:00
|
|
|
opts.InstallFlags(flags)
|
2017-01-23 12:23:07 +01:00
|
|
|
installConfigFlags(opts.daemonConfig, flags)
|
2016-06-22 18:36:51 -04:00
|
|
|
installServiceFlags(flags)
|
2015-05-05 00:18:28 -04:00
|
|
|
|
2016-06-21 16:42:47 -04:00
|
|
|
return cmd
|
|
|
|
}
|
2014-06-27 15:55:20 +02:00
|
|
|
|
2017-06-01 13:34:31 -07:00
|
|
|
func runDaemon(opts *daemonOptions) error {
|
2016-06-21 16:42:47 -04:00
|
|
|
if opts.version {
|
|
|
|
showVersion()
|
|
|
|
return nil
|
2014-08-01 11:34:06 -06:00
|
|
|
}
|
2016-04-22 17:16:14 -07:00
|
|
|
|
2016-08-03 12:20:46 -04:00
|
|
|
daemonCli := NewDaemonCli()
|
|
|
|
|
2016-11-04 12:42:21 -07:00
|
|
|
// Windows specific settings as these are not defaulted.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if opts.daemonConfig.Pidfile == "" {
|
|
|
|
opts.daemonConfig.Pidfile = filepath.Join(opts.daemonConfig.Root, "docker.pid")
|
|
|
|
}
|
|
|
|
if opts.configFile == "" {
|
|
|
|
opts.configFile = filepath.Join(opts.daemonConfig.Root, `config\daemon.json`)
|
|
|
|
}
|
2016-10-17 11:48:51 -07:00
|
|
|
}
|
|
|
|
|
2016-04-22 17:16:14 -07:00
|
|
|
// On Windows, this may be launching as a service or with an option to
|
|
|
|
// register the service.
|
2017-03-08 15:18:30 -08:00
|
|
|
stop, runAsService, err := initService(daemonCli)
|
2016-04-22 17:16:14 -07:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-06-21 16:42:47 -04:00
|
|
|
if stop {
|
|
|
|
return nil
|
2016-04-22 17:16:14 -07:00
|
|
|
}
|
2016-06-21 16:42:47 -04:00
|
|
|
|
2017-03-08 15:18:30 -08:00
|
|
|
// If Windows SCM manages the service - no need for PID files
|
|
|
|
if runAsService {
|
|
|
|
opts.daemonConfig.Pidfile = ""
|
|
|
|
}
|
|
|
|
|
2016-08-03 12:20:46 -04:00
|
|
|
err = daemonCli.start(opts)
|
2016-06-21 16:42:47 -04:00
|
|
|
notifyShutdown(err)
|
|
|
|
return err
|
2013-03-13 00:29:40 -07:00
|
|
|
}
|
|
|
|
|
2013-08-06 20:51:12 -07:00
|
|
|
func showVersion() {
|
2016-10-06 07:09:54 -07:00
|
|
|
fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
|
2013-08-06 20:51:12 -07:00
|
|
|
}
|
2016-06-21 16:42:47 -04:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set terminal emulation based on platform as required.
|
|
|
|
_, stdout, stderr := term.StdStreams()
|
2017-03-30 15:36:42 -07:00
|
|
|
|
|
|
|
// @jhowardmsft - maybe there is a historic reason why on non-Windows, stderr is used
|
|
|
|
// here. However, on Windows it makes no sense and there is no need.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
logrus.SetOutput(stdout)
|
|
|
|
} else {
|
|
|
|
logrus.SetOutput(stderr)
|
|
|
|
}
|
2016-06-21 16:42:47 -04:00
|
|
|
|
|
|
|
cmd := newDaemonCommand()
|
|
|
|
cmd.SetOutput(stdout)
|
|
|
|
if err := cmd.Execute(); err != nil {
|
2016-08-03 12:20:46 -04:00
|
|
|
fmt.Fprintf(stderr, "%s\n", err)
|
|
|
|
os.Exit(1)
|
2016-06-21 16:42:47 -04:00
|
|
|
}
|
|
|
|
}
|