docker.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/dotcloud/docker"
  6. "github.com/dotcloud/docker/engine"
  7. "github.com/dotcloud/docker/sysinit"
  8. "github.com/dotcloud/docker/utils"
  9. "log"
  10. "os"
  11. "strings"
  12. )
  13. var (
  14. GITCOMMIT string
  15. VERSION string
  16. )
  17. func main() {
  18. if selfPath := utils.SelfPath(); selfPath == "/sbin/init" || selfPath == "/.dockerinit" {
  19. // Running in init mode
  20. sysinit.SysInit()
  21. return
  22. }
  23. var (
  24. flVersion = flag.Bool("v", false, "Print version information and quit")
  25. flDaemon = flag.Bool("d", false, "Enable daemon mode")
  26. flDebug = flag.Bool("D", false, "Enable debug mode")
  27. flAutoRestart = flag.Bool("r", true, "Restart previously running containers")
  28. bridgeName = flag.String("b", "", "Attach containers to a pre-existing network bridge; use 'none' to disable container networking")
  29. pidfile = flag.String("p", "/var/run/docker.pid", "Path to use for daemon PID file")
  30. flRoot = flag.String("g", "/var/lib/docker", "Path to use as the root of the docker runtime")
  31. flEnableCors = flag.Bool("api-enable-cors", false, "Enable CORS headers in the remote API")
  32. flDns = flag.String("dns", "", "Force docker to use specific DNS servers")
  33. flEnableIptables = flag.Bool("iptables", true, "Disable docker's addition of iptables rules")
  34. flDefaultIp = flag.String("ip", "0.0.0.0", "Default IP address to use when binding container ports")
  35. flInterContainerComm = flag.Bool("icc", true, "Enable inter-container communication")
  36. flGraphDriver = flag.String("s", "", "Force the docker runtime to use a specific storage driver")
  37. flHosts = docker.NewListOpts(docker.ValidateHost)
  38. )
  39. flag.Var(&flHosts, "H", "Multiple tcp://host:port or unix://path/to/socket to bind in daemon mode, single connection otherwise")
  40. flag.Parse()
  41. if *flVersion {
  42. showVersion()
  43. return
  44. }
  45. if flHosts.Len() == 0 {
  46. // If we do not have a host, default to unix socket
  47. flHosts.Set(fmt.Sprintf("unix://%s", docker.DEFAULTUNIXSOCKET))
  48. }
  49. if *flDebug {
  50. os.Setenv("DEBUG", "1")
  51. }
  52. docker.GITCOMMIT = GITCOMMIT
  53. docker.VERSION = VERSION
  54. if *flDaemon {
  55. if flag.NArg() != 0 {
  56. flag.Usage()
  57. return
  58. }
  59. eng, err := engine.New(*flRoot)
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. // Load plugin: httpapi
  64. job := eng.Job("initapi")
  65. job.Setenv("Pidfile", *pidfile)
  66. job.Setenv("Root", *flRoot)
  67. job.SetenvBool("AutoRestart", *flAutoRestart)
  68. job.SetenvBool("EnableCors", *flEnableCors)
  69. job.Setenv("Dns", *flDns)
  70. job.SetenvBool("EnableIptables", *flEnableIptables)
  71. job.Setenv("BridgeIface", *bridgeName)
  72. job.Setenv("DefaultIp", *flDefaultIp)
  73. job.SetenvBool("InterContainerCommunication", *flInterContainerComm)
  74. job.Setenv("GraphDriver", *flGraphDriver)
  75. if err := job.Run(); err != nil {
  76. log.Fatal(err)
  77. }
  78. // Serve api
  79. job = eng.Job("serveapi", flHosts.GetAll()...)
  80. job.SetenvBool("Logging", true)
  81. if err := job.Run(); err != nil {
  82. log.Fatal(err)
  83. }
  84. } else {
  85. if flHosts.Len() > 1 {
  86. log.Fatal("Please specify only one -H")
  87. }
  88. protoAddrParts := strings.SplitN(flHosts.GetAll()[0], "://", 2)
  89. if err := docker.ParseCommands(protoAddrParts[0], protoAddrParts[1], flag.Args()...); err != nil {
  90. if sterr, ok := err.(*utils.StatusError); ok {
  91. if sterr.Status != "" {
  92. log.Println(sterr.Status)
  93. }
  94. os.Exit(sterr.StatusCode)
  95. }
  96. log.Fatal(err)
  97. }
  98. }
  99. }
  100. func showVersion() {
  101. fmt.Printf("Docker version %s, build %s\n", VERSION, GITCOMMIT)
  102. }