Explorar el Código

switch to default 127.0.0.1, and mixed the two flags in one. -h

Victor Vieux hace 12 años
padre
commit
13f1939a63
Se han modificado 4 ficheros con 33 adiciones y 19 borrados
  1. 9 9
      commands.go
  2. 21 6
      docker/docker.go
  3. 1 2
      docs/sources/commandline/cli.rst
  4. 2 2
      docs/sources/use/basics.rst

+ 9 - 9
commands.go

@@ -30,8 +30,8 @@ var (
 	GIT_COMMIT string
 )
 
-func ParseCommands(host string, port int, args ...string) error {
-	cli := NewDockerCli(host, port)
+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(host string, port int, args ...string) error {
 }
 
 func (cli *DockerCli) CmdHelp(args ...string) error {
-	help := "Usage: docker [OPTIONS] COMMAND [arg...]\n  -host=\"0.0.0.0\": Host to bind/connect to\n  -port=4243: Port to listen/connect to\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", cli.host, cli.port)+path, params)
+	req, err := http.NewRequest(method, fmt.Sprintf("http://%s:%d", cli.addr, cli.port)+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%s", cli.host, cli.port, path), in)
+	req, err := http.NewRequest(method, fmt.Sprintf("http://%s:%d%s", cli.addr, cli.port, path), in)
 	if err != nil {
 		return err
 	}
@@ -1235,7 +1235,7 @@ func (cli *DockerCli) hijack(method, path string, setRawTerminal bool) error {
 		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
 }

+ 21 - 6
docker/docker.go

@@ -10,6 +10,7 @@ import (
 	"os"
 	"os/signal"
 	"strconv"
+	"strings"
 	"syscall"
 )
 
@@ -23,20 +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")
-	port := flag.Int("port", 4243, "Port to listen/connect to")
-	host := flag.String("host", "0.0.0.0", "Host bind/connect to")
+	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")
 	}
@@ -46,12 +61,12 @@ func main() {
 			flag.Usage()
 			return
 		}
-		if err := daemon(*pidfile, *host, *port, *flAutoRestart); err != nil {
+		if err := daemon(*pidfile, host, port, *flAutoRestart); err != nil {
 			log.Fatal(err)
 			os.Exit(-1)
 		}
 	} else {
-		if err := docker.ParseCommands(*host, *port, flag.Args()...); err != nil {
+		if err := docker.ParseCommands(host, port, flag.Args()...); err != nil {
 			log.Fatal(err)
 			os.Exit(-1)
 		}
@@ -85,7 +100,7 @@ func removePidFile(pidfile string) {
 	}
 }
 
-func daemon(pidfile, host string, port int, autoRestart bool) error {
+func daemon(pidfile, addr string, port int, autoRestart bool) error {
 	if err := createPidFile(pidfile); err != nil {
 		log.Fatal(err)
 	}
@@ -105,5 +120,5 @@ func daemon(pidfile, host string, port int, autoRestart bool) error {
 		return err
 	}
 
-	return docker.ListenAndServe(fmt.Sprintf("%s:%d", host, port), server, true)
+	return docker.ListenAndServe(fmt.Sprintf("%s:%d", addr, port), server, true)
 }

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

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

+ 2 - 2
docs/sources/use/basics.rst

@@ -42,9 +42,9 @@ use -host and -port on both deamon and client
 .. code-block:: bash
 
    # Run docker in daemon mode
-   sudo <path to>/docker -host 127.0.0.1 -port 5555 &
+   sudo <path to>/docker -h 0.0.0.0:5555 &
    # Download a base image
-   docker -port 5555 pull base
+   docker -h :5555 pull base
 
 
 Starting a long-running worker process