docker.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. bridgeIp = flag.String("bip", "", "Use this CIDR notation address for the network bridge's IP, not compatible with -b")
  30. pidfile = flag.String("p", "/var/run/docker.pid", "Path to use for daemon PID file")
  31. flRoot = flag.String("g", "/var/lib/docker", "Path to use as the root of the docker runtime")
  32. flEnableCors = flag.Bool("api-enable-cors", false, "Enable CORS headers in the remote API")
  33. flDns = docker.NewListOpts(docker.ValidateIp4Address)
  34. flEnableIptables = flag.Bool("iptables", true, "Disable docker's addition of iptables rules")
  35. flDefaultIp = flag.String("ip", "0.0.0.0", "Default IP address to use when binding container ports")
  36. flInterContainerComm = flag.Bool("icc", true, "Enable inter-container communication")
  37. flGraphDriver = flag.String("s", "", "Force the docker runtime to use a specific storage driver")
  38. flHosts = docker.NewListOpts(docker.ValidateHost)
  39. flMtu = flag.Int("mtu", docker.DefaultNetworkMtu, "Set the containers network mtu")
  40. )
  41. flag.Var(&flDns, "dns", "Force docker to use specific DNS servers")
  42. flag.Var(&flHosts, "H", "Multiple tcp://host:port or unix://path/to/socket to bind in daemon mode, single connection otherwise")
  43. flag.Parse()
  44. if *flVersion {
  45. showVersion()
  46. return
  47. }
  48. if flHosts.Len() == 0 {
  49. // If we do not have a host, default to unix socket
  50. flHosts.Set(fmt.Sprintf("unix://%s", docker.DEFAULTUNIXSOCKET))
  51. }
  52. if *bridgeName != "" && *bridgeIp != "" {
  53. log.Fatal("You specified -b & -bip, mutually exclusive options. Please specify only one.")
  54. }
  55. if *flDebug {
  56. os.Setenv("DEBUG", "1")
  57. }
  58. docker.GITCOMMIT = GITCOMMIT
  59. docker.VERSION = VERSION
  60. if *flDaemon {
  61. if flag.NArg() != 0 {
  62. flag.Usage()
  63. return
  64. }
  65. eng, err := engine.New(*flRoot)
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. // Load plugin: httpapi
  70. job := eng.Job("initapi")
  71. job.Setenv("Pidfile", *pidfile)
  72. job.Setenv("Root", *flRoot)
  73. job.SetenvBool("AutoRestart", *flAutoRestart)
  74. job.SetenvBool("EnableCors", *flEnableCors)
  75. job.SetenvList("Dns", flDns.GetAll())
  76. job.SetenvBool("EnableIptables", *flEnableIptables)
  77. job.Setenv("BridgeIface", *bridgeName)
  78. job.Setenv("BridgeIp", *bridgeIp)
  79. job.Setenv("DefaultIp", *flDefaultIp)
  80. job.SetenvBool("InterContainerCommunication", *flInterContainerComm)
  81. job.Setenv("GraphDriver", *flGraphDriver)
  82. job.SetenvInt("Mtu", *flMtu)
  83. if err := job.Run(); err != nil {
  84. log.Fatal(err)
  85. }
  86. // Serve api
  87. job = eng.Job("serveapi", flHosts.GetAll()...)
  88. job.SetenvBool("Logging", true)
  89. if err := job.Run(); err != nil {
  90. log.Fatal(err)
  91. }
  92. } else {
  93. if flHosts.Len() > 1 {
  94. log.Fatal("Please specify only one -H")
  95. }
  96. protoAddrParts := strings.SplitN(flHosts.GetAll()[0], "://", 2)
  97. if err := docker.ParseCommands(protoAddrParts[0], protoAddrParts[1], flag.Args()...); err != nil {
  98. if sterr, ok := err.(*utils.StatusError); ok {
  99. if sterr.Status != "" {
  100. log.Println(sterr.Status)
  101. }
  102. os.Exit(sterr.StatusCode)
  103. }
  104. log.Fatal(err)
  105. }
  106. }
  107. }
  108. func showVersion() {
  109. fmt.Printf("Docker version %s, build %s\n", VERSION, GITCOMMIT)
  110. }