Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions pkg/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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...)
Expand Down