docker.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strings"
  7. "github.com/dotcloud/docker/api"
  8. "github.com/dotcloud/docker/builtins"
  9. "github.com/dotcloud/docker/dockerversion"
  10. "github.com/dotcloud/docker/engine"
  11. flag "github.com/dotcloud/docker/pkg/mflag"
  12. "github.com/dotcloud/docker/pkg/opts"
  13. "github.com/dotcloud/docker/sysinit"
  14. "github.com/dotcloud/docker/utils"
  15. )
  16. func main() {
  17. if selfPath := utils.SelfPath(); strings.Contains(selfPath, ".dockerinit") {
  18. // Running in init mode
  19. sysinit.SysInit()
  20. return
  21. }
  22. var (
  23. flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
  24. flDaemon = flag.Bool([]string{"d", "-daemon"}, false, "Enable daemon mode")
  25. flDebug = flag.Bool([]string{"D", "-debug"}, false, "Enable debug mode")
  26. flAutoRestart = flag.Bool([]string{"r", "-restart"}, true, "Restart previously running containers")
  27. bridgeName = flag.String([]string{"b", "-bridge"}, "", "Attach containers to a pre-existing network bridge; use 'none' to disable container networking")
  28. bridgeIp = flag.String([]string{"#bip", "-bip"}, "", "Use this CIDR notation address for the network bridge's IP, not compatible with -b")
  29. pidfile = flag.String([]string{"p", "-pidfile"}, "/var/run/docker.pid", "Path to use for daemon PID file")
  30. flRoot = flag.String([]string{"g", "-graph"}, "/var/lib/docker", "Path to use as the root of the docker runtime")
  31. flEnableCors = flag.Bool([]string{"#api-enable-cors", "-api-enable-cors"}, false, "Enable CORS headers in the remote API")
  32. flDns = opts.NewListOpts(opts.ValidateIp4Address)
  33. flEnableIptables = flag.Bool([]string{"#iptables", "-iptables"}, true, "Disable docker's addition of iptables rules")
  34. flEnableIpForward = flag.Bool([]string{"#ip-forward", "-ip-forward"}, true, "Disable enabling of net.ipv4.ip_forward")
  35. flDefaultIp = flag.String([]string{"#ip", "-ip"}, "0.0.0.0", "Default IP address to use when binding container ports")
  36. flInterContainerComm = flag.Bool([]string{"#icc", "-icc"}, true, "Enable inter-container communication")
  37. flGraphDriver = flag.String([]string{"s", "-storage-driver"}, "", "Force the docker runtime to use a specific storage driver")
  38. flExecDriver = flag.String([]string{"e", "-exec-driver"}, "native", "Force the docker runtime to use a specific exec driver")
  39. flHosts = opts.NewListOpts(api.ValidateHost)
  40. flMtu = flag.Int([]string{"#mtu", "-mtu"}, 0, "Set the containers network MTU; if no value is provided: default to the default route MTU or 1500 if no default route is available")
  41. )
  42. flag.Var(&flDns, []string{"#dns", "-dns"}, "Force docker to use specific DNS servers")
  43. flag.Var(&flHosts, []string{"H", "-host"}, "tcp://host:port, unix://path/to/socket, fd://* or fd://socketfd to use in daemon mode. Multiple sockets can be specified")
  44. flag.Parse()
  45. if *flVersion {
  46. showVersion()
  47. return
  48. }
  49. if flHosts.Len() == 0 {
  50. defaultHost := os.Getenv("DOCKER_HOST")
  51. if defaultHost == "" || *flDaemon {
  52. // If we do not have a host, default to unix socket
  53. defaultHost = fmt.Sprintf("unix://%s", api.DEFAULTUNIXSOCKET)
  54. }
  55. if _, err := api.ValidateHost(defaultHost); err != nil {
  56. log.Fatal(err)
  57. }
  58. flHosts.Set(defaultHost)
  59. }
  60. if *bridgeName != "" && *bridgeIp != "" {
  61. log.Fatal("You specified -b & --bip, mutually exclusive options. Please specify only one.")
  62. }
  63. if *flDebug {
  64. os.Setenv("DEBUG", "1")
  65. }
  66. if *flDaemon {
  67. if flag.NArg() != 0 {
  68. flag.Usage()
  69. return
  70. }
  71. eng, err := engine.New(*flRoot)
  72. if err != nil {
  73. log.Fatal(err)
  74. }
  75. // Load builtins
  76. builtins.Register(eng)
  77. // load the daemon in the background so we can immediately start
  78. // the http api so that connections don't fail while the daemon
  79. // is booting
  80. go func() {
  81. // Load plugin: httpapi
  82. job := eng.Job("initserver")
  83. job.Setenv("Pidfile", *pidfile)
  84. job.Setenv("Root", *flRoot)
  85. job.SetenvBool("AutoRestart", *flAutoRestart)
  86. job.SetenvList("Dns", flDns.GetAll())
  87. job.SetenvBool("EnableIptables", *flEnableIptables)
  88. job.SetenvBool("EnableIpForward", *flEnableIpForward)
  89. job.Setenv("BridgeIface", *bridgeName)
  90. job.Setenv("BridgeIP", *bridgeIp)
  91. job.Setenv("DefaultIp", *flDefaultIp)
  92. job.SetenvBool("InterContainerCommunication", *flInterContainerComm)
  93. job.Setenv("GraphDriver", *flGraphDriver)
  94. job.Setenv("ExecDriver", *flExecDriver)
  95. job.SetenvInt("Mtu", *flMtu)
  96. if err := job.Run(); err != nil {
  97. log.Fatal(err)
  98. }
  99. // after the daemon is done setting up we can tell the api to start
  100. // accepting connections
  101. if err := eng.Job("acceptconnections").Run(); err != nil {
  102. log.Fatal(err)
  103. }
  104. }()
  105. // Serve api
  106. job := eng.Job("serveapi", flHosts.GetAll()...)
  107. job.SetenvBool("Logging", true)
  108. job.SetenvBool("EnableCors", *flEnableCors)
  109. job.Setenv("Version", dockerversion.VERSION)
  110. if err := job.Run(); err != nil {
  111. log.Fatal(err)
  112. }
  113. } else {
  114. if flHosts.Len() > 1 {
  115. log.Fatal("Please specify only one -H")
  116. }
  117. protoAddrParts := strings.SplitN(flHosts.GetAll()[0], "://", 2)
  118. if err := api.ParseCommands(protoAddrParts[0], protoAddrParts[1], flag.Args()...); err != nil {
  119. if sterr, ok := err.(*utils.StatusError); ok {
  120. if sterr.Status != "" {
  121. log.Println(sterr.Status)
  122. }
  123. os.Exit(sterr.StatusCode)
  124. }
  125. log.Fatal(err)
  126. }
  127. }
  128. }
  129. func showVersion() {
  130. fmt.Printf("Docker version %s, build %s\n", dockerversion.VERSION, dockerversion.GITCOMMIT)
  131. }