Merge pull request #2132 from tonistiigi/ssh-mux

connhelper: use ssh multiplexing
This commit is contained in:
Sebastiaan van Stijn 2020-01-09 23:12:25 +01:00 committed by GitHub
commit a4f60165b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -5,7 +5,10 @@ import (
"context" "context"
"net" "net"
"net/url" "net/url"
"os"
"strconv"
"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/connhelper/commandconn" "github.com/docker/cli/cli/connhelper/commandconn"
"github.com/docker/cli/cli/connhelper/ssh" "github.com/docker/cli/cli/connhelper/ssh"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -34,7 +37,7 @@ func GetConnectionHelper(daemonURL string) (*ConnectionHelper, error) {
} }
return &ConnectionHelper{ return &ConnectionHelper{
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) { Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
return commandconn.New(ctx, "ssh", append(sp.Args(), []string{"--", "docker", "system", "dial-stdio"}...)...) return commandconn.New(ctx, "ssh", append(multiplexingArgs(), append(sp.Args(), []string{"--", "docker", "system", "dial-stdio"}...)...)...)
}, },
Host: "http://docker", Host: "http://docker",
}, nil }, nil
@ -53,3 +56,19 @@ func GetCommandConnectionHelper(cmd string, flags ...string) (*ConnectionHelper,
Host: "http://docker", Host: "http://docker",
}, nil }, nil
} }
func multiplexingArgs() []string {
if v := os.Getenv("DOCKER_SSH_NO_MUX"); v != "" {
if b, err := strconv.ParseBool(v); err == nil && b {
return nil
}
}
if err := os.MkdirAll(config.Dir(), 0700); err != nil {
return nil
}
args := []string{"-o", "ControlMaster=auto", "-o", "ControlPath=" + config.Dir() + "/%r@%h:%p"}
if v := os.Getenv("DOCKER_SSH_MUX_PERSIST"); v != "" {
args = append(args, "-o", "ControlPersist="+v)
}
return args
}

View File

@ -86,6 +86,8 @@ by the `docker` command line:
* `DOCKER_TMPDIR` Location for temporary Docker files. * `DOCKER_TMPDIR` Location for temporary Docker files.
* `DOCKER_CONTEXT` Specify the context to use (overrides DOCKER_HOST env var and default context set with "docker context use") * `DOCKER_CONTEXT` Specify the context to use (overrides DOCKER_HOST env var and default context set with "docker context use")
* `DOCKER_DEFAULT_PLATFORM` Specify the default platform for the commands that take the `--platform` flag. * `DOCKER_DEFAULT_PLATFORM` Specify the default platform for the commands that take the `--platform` flag.
* `DOCKER_SSH_NO_MUX` If set will turn off SSH multiplexing when connecting to daemon through SSH.
* `DOCKER_SSH_MUX_PERSIST` Set a duration for keeping SSH multiplexing socket alive between commands (e.g `60s`).
Because Docker is developed using Go, you can also use any environment Because Docker is developed using Go, you can also use any environment
variables used by the Go runtime. In particular, you may find these useful: variables used by the Go runtime. In particular, you may find these useful: