docker.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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", false, "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. log.Fatal(err)
  75. os.Exit(-1)
  76. }
  77. }
  78. }
  79. func showVersion() {
  80. fmt.Printf("Docker version %s, build %s\n", VERSION, GITCOMMIT)
  81. }
  82. func createPidFile(pidfile string) error {
  83. if pidString, err := ioutil.ReadFile(pidfile); err == nil {
  84. pid, err := strconv.Atoi(string(pidString))
  85. if err == nil {
  86. if _, err := os.Stat(fmt.Sprintf("/proc/%d/", pid)); err == nil {
  87. return fmt.Errorf("pid file found, ensure docker is not running or delete %s", pidfile)
  88. }
  89. }
  90. }
  91. file, err := os.Create(pidfile)
  92. if err != nil {
  93. return err
  94. }
  95. defer file.Close()
  96. _, err = fmt.Fprintf(file, "%d", os.Getpid())
  97. return err
  98. }
  99. func removePidFile(pidfile string) {
  100. if err := os.Remove(pidfile); err != nil {
  101. log.Printf("Error removing %s: %s", pidfile, err)
  102. }
  103. }
  104. func daemon(pidfile string, flGraphPath string, protoAddrs []string, autoRestart, enableCors bool, flDns string) error {
  105. if err := createPidFile(pidfile); err != nil {
  106. log.Fatal(err)
  107. }
  108. defer removePidFile(pidfile)
  109. c := make(chan os.Signal, 1)
  110. signal.Notify(c, os.Interrupt, os.Kill, os.Signal(syscall.SIGTERM))
  111. go func() {
  112. sig := <-c
  113. log.Printf("Received signal '%v', exiting\n", sig)
  114. removePidFile(pidfile)
  115. os.Exit(0)
  116. }()
  117. var dns []string
  118. if flDns != "" {
  119. dns = []string{flDns}
  120. }
  121. server, err := docker.NewServer(flGraphPath, autoRestart, enableCors, dns)
  122. if err != nil {
  123. return err
  124. }
  125. chErrors := make(chan error, len(protoAddrs))
  126. for _, protoAddr := range protoAddrs {
  127. protoAddrParts := strings.SplitN(protoAddr, "://", 2)
  128. if protoAddrParts[0] == "unix" {
  129. syscall.Unlink(protoAddrParts[1])
  130. } else if protoAddrParts[0] == "tcp" {
  131. if !strings.HasPrefix(protoAddrParts[1], "127.0.0.1") {
  132. log.Println("/!\\ DON'T BIND ON ANOTHER IP ADDRESS THAN 127.0.0.1 IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
  133. }
  134. } else {
  135. log.Fatal("Invalid protocol format.")
  136. os.Exit(-1)
  137. }
  138. go func() {
  139. chErrors <- docker.ListenAndServe(protoAddrParts[0], protoAddrParts[1], server, true)
  140. }()
  141. }
  142. for i := 0; i < len(protoAddrs); i += 1 {
  143. err := <-chErrors
  144. if err != nil {
  145. return err
  146. }
  147. }
  148. return nil
  149. }