docker.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. defaultHost := os.Getenv("DOCKER_HOST")
  50. if defaultHost == "" || *flDaemon {
  51. // If we do not have a host, default to unix socket
  52. defaultHost = fmt.Sprintf("unix://%s", docker.DEFAULTUNIXSOCKET)
  53. }
  54. flHosts.Set(defaultHost)
  55. }
  56. if *bridgeName != "" && *bridgeIp != "" {
  57. log.Fatal("You specified -b & -bip, mutually exclusive options. Please specify only one.")
  58. }
  59. if *flDebug {
  60. os.Setenv("DEBUG", "1")
  61. }
  62. docker.GITCOMMIT = GITCOMMIT
  63. docker.VERSION = VERSION
  64. if *flDaemon {
  65. if flag.NArg() != 0 {
  66. flag.Usage()
  67. return
  68. }
  69. eng, err := engine.New(*flRoot)
  70. if err != nil {
  71. log.Fatal(err)
  72. }
  73. // Load plugin: httpapi
  74. job := eng.Job("initapi")
  75. job.Setenv("Pidfile", *pidfile)
  76. job.Setenv("Root", *flRoot)
  77. job.SetenvBool("AutoRestart", *flAutoRestart)
  78. job.SetenvBool("EnableCors", *flEnableCors)
  79. job.SetenvList("Dns", flDns.GetAll())
  80. job.SetenvBool("EnableIptables", *flEnableIptables)
  81. job.Setenv("BridgeIface", *bridgeName)
  82. job.Setenv("BridgeIp", *bridgeIp)
  83. job.Setenv("DefaultIp", *flDefaultIp)
  84. job.SetenvBool("InterContainerCommunication", *flInterContainerComm)
  85. job.Setenv("GraphDriver", *flGraphDriver)
  86. job.SetenvInt("Mtu", *flMtu)
  87. if err := job.Run(); err != nil {
  88. log.Fatal(err)
  89. }
  90. // Serve api
  91. job = eng.Job("serveapi", flHosts.GetAll()...)
  92. job.SetenvBool("Logging", true)
  93. if err := job.Run(); err != nil {
  94. log.Fatal(err)
  95. }
  96. } else {
  97. if flHosts.Len() > 1 {
  98. log.Fatal("Please specify only one -H")
  99. }
  100. protoAddrParts := strings.SplitN(flHosts.GetAll()[0], "://", 2)
  101. if err := docker.ParseCommands(protoAddrParts[0], protoAddrParts[1], flag.Args()...); err != nil {
  102. if sterr, ok := err.(*utils.StatusError); ok {
  103. if sterr.Status != "" {
  104. log.Println(sterr.Status)
  105. }
  106. os.Exit(sterr.StatusCode)
  107. }
  108. log.Fatal(err)
  109. }
  110. }
  111. }
  112. func showVersion() {
  113. fmt.Printf("Docker version %s, build %s\n", VERSION, GITCOMMIT)
  114. }