docker.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/dotcloud/docker"
  6. "github.com/dotcloud/docker/utils"
  7. "io/ioutil"
  8. "log"
  9. "os"
  10. "os/signal"
  11. "strconv"
  12. "strings"
  13. "syscall"
  14. )
  15. var (
  16. GITCOMMIT string
  17. VERSION string
  18. )
  19. func main() {
  20. if selfPath := utils.SelfPath(); selfPath == "/sbin/init" || selfPath == "/.dockerinit" {
  21. // Running in init mode
  22. docker.SysInit()
  23. return
  24. }
  25. // FIXME: Switch d and D ? (to be more sshd like)
  26. flVersion := flag.Bool("v", false, "Print version information and quit")
  27. flDaemon := flag.Bool("d", false, "Daemon mode")
  28. flDebug := flag.Bool("D", false, "Debug mode")
  29. flAutoRestart := flag.Bool("r", true, "Restart previously running containers")
  30. bridgeName := flag.String("b", "", "Attach containers to a pre-existing network bridge. Use 'none' to disable container networking")
  31. pidfile := flag.String("p", "/var/run/docker.pid", "File containing process PID")
  32. flGraphPath := flag.String("g", "/var/lib/docker", "Path to graph storage base dir.")
  33. flEnableCors := flag.Bool("api-enable-cors", false, "Enable CORS requests in the remote api.")
  34. flDns := flag.String("dns", "", "Set custom dns servers")
  35. flHosts := docker.ListOpts{fmt.Sprintf("unix://%s", docker.DEFAULTUNIXSOCKET)}
  36. flag.Var(&flHosts, "H", "tcp://host:port to bind/connect to or unix://path/to/socket to use")
  37. flag.Parse()
  38. if *flVersion {
  39. showVersion()
  40. return
  41. }
  42. if len(flHosts) > 1 {
  43. flHosts = flHosts[1:] //trick to display a nice default value in the usage
  44. }
  45. for i, flHost := range flHosts {
  46. flHosts[i] = utils.ParseHost(docker.DEFAULTHTTPHOST, docker.DEFAULTHTTPPORT, flHost)
  47. }
  48. if *bridgeName != "" {
  49. docker.NetworkBridgeIface = *bridgeName
  50. } else {
  51. docker.NetworkBridgeIface = docker.DefaultNetworkBridge
  52. }
  53. if *flDebug {
  54. os.Setenv("DEBUG", "1")
  55. }
  56. docker.GITCOMMIT = GITCOMMIT
  57. docker.VERSION = VERSION
  58. if *flDaemon {
  59. if flag.NArg() != 0 {
  60. flag.Usage()
  61. return
  62. }
  63. if err := daemon(*pidfile, *flGraphPath, flHosts, *flAutoRestart, *flEnableCors, *flDns); err != nil {
  64. log.Fatal(err)
  65. os.Exit(-1)
  66. }
  67. } else {
  68. if len(flHosts) > 1 {
  69. log.Fatal("Please specify only one -H")
  70. return
  71. }
  72. protoAddrParts := strings.SplitN(flHosts[0], "://", 2)
  73. if err := docker.ParseCommands(protoAddrParts[0], protoAddrParts[1], flag.Args()...); err != nil {
  74. if sterr, ok := err.(*utils.StatusError); ok {
  75. os.Exit(sterr.Status)
  76. }
  77. log.Fatal(err)
  78. os.Exit(-1)
  79. }
  80. }
  81. }
  82. func showVersion() {
  83. fmt.Printf("Docker version %s, build %s\n", VERSION, GITCOMMIT)
  84. }
  85. func createPidFile(pidfile string) error {
  86. if pidString, err := ioutil.ReadFile(pidfile); err == nil {
  87. pid, err := strconv.Atoi(string(pidString))
  88. if err == nil {
  89. if _, err := os.Stat(fmt.Sprintf("/proc/%d/", pid)); err == nil {
  90. return fmt.Errorf("pid file found, ensure docker is not running or delete %s", pidfile)
  91. }
  92. }
  93. }
  94. file, err := os.Create(pidfile)
  95. if err != nil {
  96. return err
  97. }
  98. defer file.Close()
  99. _, err = fmt.Fprintf(file, "%d", os.Getpid())
  100. return err
  101. }
  102. func removePidFile(pidfile string) {
  103. if err := os.Remove(pidfile); err != nil {
  104. log.Printf("Error removing %s: %s", pidfile, err)
  105. }
  106. }
  107. func daemon(pidfile string, flGraphPath string, protoAddrs []string, autoRestart, enableCors bool, flDns string) error {
  108. if err := createPidFile(pidfile); err != nil {
  109. log.Fatal(err)
  110. }
  111. defer removePidFile(pidfile)
  112. c := make(chan os.Signal, 1)
  113. signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
  114. go func() {
  115. sig := <-c
  116. log.Printf("Received signal '%v', exiting\n", sig)
  117. removePidFile(pidfile)
  118. os.Exit(0)
  119. }()
  120. var dns []string
  121. if flDns != "" {
  122. dns = []string{flDns}
  123. }
  124. server, err := docker.NewServer(flGraphPath, autoRestart, enableCors, dns)
  125. if err != nil {
  126. return err
  127. }
  128. chErrors := make(chan error, len(protoAddrs))
  129. for _, protoAddr := range protoAddrs {
  130. protoAddrParts := strings.SplitN(protoAddr, "://", 2)
  131. if protoAddrParts[0] == "unix" {
  132. syscall.Unlink(protoAddrParts[1])
  133. } else if protoAddrParts[0] == "tcp" {
  134. if !strings.HasPrefix(protoAddrParts[1], "127.0.0.1") {
  135. log.Println("/!\\ DON'T BIND ON ANOTHER IP ADDRESS THAN 127.0.0.1 IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
  136. }
  137. } else {
  138. log.Fatal("Invalid protocol format.")
  139. os.Exit(-1)
  140. }
  141. go func() {
  142. chErrors <- docker.ListenAndServe(protoAddrParts[0], protoAddrParts[1], server, true)
  143. }()
  144. }
  145. for i := 0; i < len(protoAddrs); i += 1 {
  146. err := <-chErrors
  147. if err != nil {
  148. return err
  149. }
  150. }
  151. return nil
  152. }