diff --git a/pkg/docker/client.go b/pkg/docker/client.go index 52445a6e..45bf0f2e 100644 --- a/pkg/docker/client.go +++ b/pkg/docker/client.go @@ -4,7 +4,10 @@ import ( "context" "fmt" "io" + "net/http" + "os" + "github.com/docker/cli/cli/connhelper" "github.com/docker/cli/cli/streams" "github.com/docker/docker/api/types/image" "github.com/docker/docker/client" @@ -28,8 +31,28 @@ func NewClient(opts Options) (*Client, error) { client.FromEnv, client.WithAPIVersionNegotiation(), } - if len(opts.Host) > 0 { - dockerOpts = append(dockerOpts, client.WithHost(opts.Host)) + + host := opts.Host + if host == "" { + host = os.Getenv("DOCKER_HOST") + } + if host != "" { + helper, err := connhelper.GetConnectionHelper(host) + if err != nil { + return nil, fmt.Errorf("cannot resolve Docker connection helper: %w", err) + } + if helper != nil { + httpClient := &http.Client{ + Transport: &http.Transport{DialContext: helper.Dialer}, + } + dockerOpts = append(dockerOpts, + client.WithHTTPClient(httpClient), + client.WithHost(helper.Host), + client.WithDialContext(helper.Dialer), + ) + } else if opts.Host != "" { + dockerOpts = append(dockerOpts, client.WithHost(opts.Host)) + } } inner, err := client.NewClientWithOpts(dockerOpts...)