docker.go 4.0 KB

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