From 47a7f770f42bc0fd9b0a594b72a8f20fb4b874d1 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 30 Jun 2015 13:59:51 -0700 Subject: [PATCH 1/2] add support for base path in docker cli -H Signed-off-by: Victor Vieux --- api/client/cli.go | 12 ++++++++++++ api/client/hijack.go | 2 +- api/client/utils.go | 2 +- pkg/parsers/parsers.go | 9 +++++++-- pkg/parsers/parsers_test.go | 12 ++++++++---- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/api/client/cli.go b/api/client/cli.go index 4ab9cfeab0..29560c26d7 100644 --- a/api/client/cli.go +++ b/api/client/cli.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "net/http" + "net/url" "reflect" "strings" "text/template" @@ -24,6 +25,8 @@ type DockerCli struct { proto string // addr holds the client address. addr string + // basePath holds the path to prepend to the requests + basePath string // configFile has the client configuration file configFile *cliconfig.ConfigFile @@ -187,6 +190,7 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, keyFile string, proto, a isTerminalIn = false isTerminalOut = false scheme = "http" + basePath = "" ) if tlsConfig != nil { @@ -215,9 +219,17 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, keyFile string, proto, a fmt.Fprintf(err, "WARNING: Error loading config file:%v\n", e) } + if proto == "tcp" { + // error is checked in pkg/parsers already + parsed, _ := url.Parse("tcp://" + addr) + addr = parsed.Host + basePath = parsed.Path + } + return &DockerCli{ proto: proto, addr: addr, + basePath: basePath, configFile: configFile, in: in, out: out, diff --git a/api/client/hijack.go b/api/client/hijack.go index 067d9b50fd..60a6b3798f 100644 --- a/api/client/hijack.go +++ b/api/client/hijack.go @@ -138,7 +138,7 @@ func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in io.Rea if err != nil { return err } - req, err := http.NewRequest(method, fmt.Sprintf("/v%s%s", api.Version, path), params) + req, err := http.NewRequest(method, fmt.Sprintf("%s/v%s%s", cli.basePath, api.Version, path), params) if err != nil { return err } diff --git a/api/client/utils.go b/api/client/utils.go index 2df5fe087c..0463c49bdb 100644 --- a/api/client/utils.go +++ b/api/client/utils.go @@ -65,7 +65,7 @@ func (cli *DockerCli) clientRequest(method, path string, in io.Reader, headers m if expectedPayload && in == nil { in = bytes.NewReader([]byte{}) } - req, err := http.NewRequest(method, fmt.Sprintf("/v%s%s", api.Version, path), in) + req, err := http.NewRequest(method, fmt.Sprintf("%s/v%s%s", cli.basePath, api.Version, path), in) if err != nil { return serverResp, err } diff --git a/pkg/parsers/parsers.go b/pkg/parsers/parsers.go index 32d87734f8..e6c8774f50 100644 --- a/pkg/parsers/parsers.go +++ b/pkg/parsers/parsers.go @@ -2,6 +2,7 @@ package parsers import ( "fmt" + "net/url" "runtime" "strconv" "strings" @@ -52,7 +53,11 @@ func ParseTCPAddr(addr string, defaultAddr string) (string, error) { return "", fmt.Errorf("Invalid proto, expected tcp: %s", addr) } - hostParts := strings.Split(addr, ":") + u, err := url.Parse("tcp://" + addr) + if err != nil { + return "", err + } + hostParts := strings.Split(u.Host, ":") if len(hostParts) != 2 { return "", fmt.Errorf("Invalid bind address format: %s", addr) } @@ -65,7 +70,7 @@ func ParseTCPAddr(addr string, defaultAddr string) (string, error) { if err != nil && p == 0 { return "", fmt.Errorf("Invalid bind address format: %s", addr) } - return fmt.Sprintf("tcp://%s:%d", host, p), nil + return fmt.Sprintf("tcp://%s:%d%s", host, p, u.Path), nil } // Get a repos name and returns the right reposName + tag|digest diff --git a/pkg/parsers/parsers_test.go b/pkg/parsers/parsers_test.go index e62aad7f90..a64e6b9462 100644 --- a/pkg/parsers/parsers_test.go +++ b/pkg/parsers/parsers_test.go @@ -14,14 +14,18 @@ func TestParseHost(t *testing.T) { "0.0.0.0": "Invalid bind address format: 0.0.0.0", "tcp://": "Invalid proto, expected tcp: ", "tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d", + "tcp:a.b.c.d/path": "Invalid bind address format: tcp:a.b.c.d/path", "udp://127.0.0.1": "Invalid bind address format: udp://127.0.0.1", "udp://127.0.0.1:2375": "Invalid bind address format: udp://127.0.0.1:2375", } valids := map[string]string{ - "0.0.0.1:5555": "tcp://0.0.0.1:5555", - ":6666": "tcp://127.0.0.1:6666", - "tcp://:7777": "tcp://127.0.0.1:7777", - "": "unix:///var/run/docker.sock", + "0.0.0.1:5555": "tcp://0.0.0.1:5555", + "0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path", + ":6666": "tcp://127.0.0.1:6666", + ":6666/path": "tcp://127.0.0.1:6666/path", + "tcp://:7777": "tcp://127.0.0.1:7777", + "tcp://:7777/path": "tcp://127.0.0.1:7777/path", + "": "unix:///var/run/docker.sock", "unix:///run/docker.sock": "unix:///run/docker.sock", "unix://": "unix:///var/run/docker.sock", "fd://": "fd://", From d2cde4fa6688b98433c7870a211b5172250c4b47 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 30 Jun 2015 14:26:40 -0700 Subject: [PATCH 2/2] update doc Signed-off-by: Victor Vieux --- docs/articles/basics.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/articles/basics.md b/docs/articles/basics.md index 340547d062..013ac1bc2a 100644 --- a/docs/articles/basics.md +++ b/docs/articles/basics.md @@ -83,12 +83,14 @@ Similarly, the Docker client can use `-H` to connect to a custom port. `-H` accepts host and port assignment in the following format: - tcp://[host][:port] or unix://path + tcp://[host][:port][path] or unix://path For example: - `tcp://host:2375` -> TCP connection on host:2375 +- `tcp://host:2375/path` -> TCP connection on + host:2375 and prepend path to all requests - `unix://path/to/socket` -> Unix socket located at `path/to/socket`