fix: use default socket path for TCP connections (#669)

Signed-off-by: Matej Vasek <mvasek@redhat.com>
This commit is contained in:
Matej Vasek 2021-11-19 21:16:23 +01:00 committed by GitHub
parent 834e8ae46c
commit ee96bef9ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -41,6 +41,14 @@ func NewClient(defaultHost string) (dockerClient client.CommonAPIClient, dockerH
_url, err = url.Parse(dockerHost)
isSSH := err == nil && _url.Scheme == "ssh"
isTCP := err == nil && _url.Scheme == "tcp"
if isTCP {
// With TCP, it's difficult to determine how to expose the daemon socket to lifecycle containers,
// so we are defaulting to standard docker location by returning empty string.
// This should work well most of the time.
dockerHost = ""
}
if !isSSH {
dockerClient, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())

View File

@ -18,7 +18,7 @@ import (
// Test that we are creating client in accordance
// with the DOCKER_HOST environment variable
func TestNewDockerClient(t *testing.T) {
func TestNewClient(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1)
defer cancel()
@ -41,6 +41,38 @@ func TestNewDockerClient(t *testing.T) {
}
}
func TestNewClient_DockerHost(t *testing.T) {
tests := []struct {
name string
dockerHostEnvVar string
expectedRemoteDockerHost string
}{
{
name: "tcp",
dockerHostEnvVar: "tcp://10.0.0.1:1234",
expectedRemoteDockerHost: "",
},
{
name: "unix",
dockerHostEnvVar: "unix:///some/path/docker.sock",
expectedRemoteDockerHost: "unix:///some/path/docker.sock",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer WithEnvVar(t, "DOCKER_HOST", tt.dockerHostEnvVar)()
_, host, err := docker.NewClient(client.DefaultDockerHost)
if err != nil {
t.Fatal(err)
}
if host != tt.expectedRemoteDockerHost {
t.Errorf("expected docker host %q, but got %q", tt.expectedRemoteDockerHost, host)
}
})
}
}
func startMockDaemon(t *testing.T, sock string) func() {
server := http.Server{}