소스 검색

Merge pull request #661 from dotcloud/573-add_host_port-feature

+ Runtime: add -H flag to listen/connect to different ip/port
Guillaume J. Charmes 12 년 전
부모
커밋
c167d603f2
4개의 변경된 파일49개의 추가작업 그리고 15개의 파일을 삭제
  1. 10 10
      commands.go
  2. 24 4
      docker/docker.go
  3. 2 1
      docs/sources/commandline/cli.rst
  4. 13 0
      docs/sources/use/basics.rst

+ 10 - 10
commands.go

@@ -30,8 +30,8 @@ var (
 	GIT_COMMIT string
 )
 
-func ParseCommands(args ...string) error {
-	cli := NewDockerCli("0.0.0.0", 4243)
+func ParseCommands(addr string, port int, args ...string) error {
+	cli := NewDockerCli(addr, port)
 
 	if len(args) > 0 {
 		methodName := "Cmd" + strings.ToUpper(args[0][:1]) + strings.ToLower(args[0][1:])
@@ -53,7 +53,7 @@ func ParseCommands(args ...string) error {
 }
 
 func (cli *DockerCli) CmdHelp(args ...string) error {
-	help := "Usage: docker COMMAND [arg...]\n\nA self-sufficient runtime for linux containers.\n\nCommands:\n"
+	help := fmt.Sprintf("Usage: docker [OPTIONS] COMMAND [arg...]\n  -H=\"%s:%d\": Host:port to bind/connect to\n\nA self-sufficient runtime for linux containers.\n\nCommands:\n", cli.addr, cli.port)
 	for cmd, description := range map[string]string{
 		"attach":  "Attach to a running container",
 		"build":   "Build a container from Dockerfile or via stdin",
@@ -1167,7 +1167,7 @@ func (cli *DockerCli) call(method, path string, data interface{}) ([]byte, int,
 		params = bytes.NewBuffer(buf)
 	}
 
-	req, err := http.NewRequest(method, fmt.Sprintf("http://%s:%d/v%f", cli.host, cli.port, API_VERSION)+path, params)
+	req, err := http.NewRequest(method, fmt.Sprintf("http://%s:%d/v%g%s", cli.addr, cli.port, API_VERSION, path), params)
 	if err != nil {
 		return nil, -1, err
 	}
@@ -1199,7 +1199,7 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
 	if (method == "POST" || method == "PUT") && in == nil {
 		in = bytes.NewReader([]byte{})
 	}
-	req, err := http.NewRequest(method, fmt.Sprintf("http://%s:%d/v%f%s", cli.host, cli.port, API_VERSION, path), in)
+	req, err := http.NewRequest(method, fmt.Sprintf("http://%s:%d/v%g%s", cli.addr, cli.port, API_VERSION, path), in)
 	if err != nil {
 		return err
 	}
@@ -1230,12 +1230,12 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
 }
 
 func (cli *DockerCli) hijack(method, path string, setRawTerminal bool) error {
-	req, err := http.NewRequest(method, fmt.Sprintf("/v%f%s", API_VERSION, path), nil)
+	req, err := http.NewRequest(method, fmt.Sprintf("/v%g%s", API_VERSION, path), nil)
 	if err != nil {
 		return err
 	}
 	req.Header.Set("Content-Type", "plain/text")
-	dial, err := net.Dial("tcp", fmt.Sprintf("%s:%d", cli.host, cli.port))
+	dial, err := net.Dial("tcp", fmt.Sprintf("%s:%d", cli.addr, cli.port))
 	if err != nil {
 		return err
 	}
@@ -1289,11 +1289,11 @@ func Subcmd(name, signature, description string) *flag.FlagSet {
 	return flags
 }
 
-func NewDockerCli(host string, port int) *DockerCli {
-	return &DockerCli{host, port}
+func NewDockerCli(addr string, port int) *DockerCli {
+	return &DockerCli{addr, port}
 }
 
 type DockerCli struct {
-	host string
+	addr string
 	port int
 }

+ 24 - 4
docker/docker.go

@@ -10,6 +10,7 @@ import (
 	"os"
 	"os/signal"
 	"strconv"
+	"strings"
 	"syscall"
 )
 
@@ -23,18 +24,34 @@ func main() {
 		docker.SysInit()
 		return
 	}
+	host:= "127.0.0.1"
+	port:= 4243
 	// FIXME: Switch d and D ? (to be more sshd like)
 	flDaemon := flag.Bool("d", false, "Daemon mode")
 	flDebug := flag.Bool("D", false, "Debug mode")
 	flAutoRestart := flag.Bool("r", false, "Restart previously running containers")
 	bridgeName := flag.String("b", "", "Attach containers to a pre-existing network bridge")
 	pidfile := flag.String("p", "/var/run/docker.pid", "File containing process PID")
+	flHost := flag.String("H", fmt.Sprintf("%s:%d", host, port), "Host:port to bind/connect to")
 	flag.Parse()
 	if *bridgeName != "" {
 		docker.NetworkBridgeIface = *bridgeName
 	} else {
 		docker.NetworkBridgeIface = docker.DefaultNetworkBridge
 	}
+
+	if strings.Contains(*flHost, ":") && len(strings.Split(*flHost, ":")) == 2 {
+		hostParts := strings.Split(*flHost, ":")
+		if hostParts[0] != "" {
+			host = hostParts[0]
+		}
+		if p, err := strconv.Atoi(hostParts[1]); err == nil {
+			port = p
+		}
+	} else if !strings.Contains(*flHost, ":") {
+		host = *flHost
+	}
+
 	if *flDebug {
 		os.Setenv("DEBUG", "1")
 	}
@@ -44,12 +61,12 @@ func main() {
 			flag.Usage()
 			return
 		}
-		if err := daemon(*pidfile, *flAutoRestart); err != nil {
+		if err := daemon(*pidfile, host, port, *flAutoRestart); err != nil {
 			log.Fatal(err)
 			os.Exit(-1)
 		}
 	} else {
-		if err := docker.ParseCommands(flag.Args()...); err != nil {
+		if err := docker.ParseCommands(host, port, flag.Args()...); err != nil {
 			log.Fatal(err)
 			os.Exit(-1)
 		}
@@ -83,7 +100,10 @@ func removePidFile(pidfile string) {
 	}
 }
 
-func daemon(pidfile string, autoRestart bool) error {
+func daemon(pidfile, addr string, port int, autoRestart bool) error {
+	if addr != "127.0.0.1" {
+		log.Println("/!\\ DON'T BIND ON ANOTHER IP ADDRESS THAN 127.0.0.1 IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
+	}
 	if err := createPidFile(pidfile); err != nil {
 		log.Fatal(err)
 	}
@@ -103,5 +123,5 @@ func daemon(pidfile string, autoRestart bool) error {
 		return err
 	}
 
-	return docker.ListenAndServe("0.0.0.0:4243", server, true)
+	return docker.ListenAndServe(fmt.Sprintf("%s:%d", addr, port), server, true)
 }

+ 2 - 1
docs/sources/commandline/cli.rst

@@ -14,7 +14,8 @@ To list available commands, either run ``docker`` with no parameters or execute
 ``docker help``::
 
   $ docker
-    Usage: docker COMMAND [arg...]
+    Usage: docker [OPTIONS] COMMAND [arg...]
+      -H="127.0.0.1:4243": Host:port to bind/connect to
 
     A self-sufficient runtime for linux containers.
 

+ 13 - 0
docs/sources/use/basics.rst

@@ -33,6 +33,19 @@ Running an interactive shell
   # allocate a tty, attach stdin and stdout
   docker run -i -t base /bin/bash
 
+Bind Docker to another host/port
+--------------------------------
+
+If you want Docker to listen to another port and bind to another ip
+use -host and -port on both deamon and client
+
+.. code-block:: bash
+
+   # Run docker in daemon mode
+   sudo <path to>/docker -H 0.0.0.0:5555 &
+   # Download a base image
+   docker -H :5555 pull base
+
 
 Starting a long-running worker process
 --------------------------------------