|
1 | 1 | package docker |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "context" |
6 | | - "encoding/json" |
7 | 5 | "errors" |
8 | 6 | "fmt" |
9 | 7 | "io" |
10 | 8 | "net" |
11 | 9 | "os" |
12 | | - "os/exec" |
13 | 10 | "strconv" |
14 | 11 | "strings" |
15 | 12 |
|
@@ -45,25 +42,26 @@ func NewAPIClient(ctx context.Context, opts ...Option) (*apiClient, error) { |
45 | 42 | opt(clientOptions) |
46 | 43 | } |
47 | 44 |
|
48 | | - // Check to see if we need to override docker host |
49 | | - host := os.Getenv("DOCKER_HOST") |
50 | | - if host == "" { |
51 | | - inspects, err := findDockerHost() |
52 | | - if err == nil { |
53 | | - for _, inspect := range inspects { |
54 | | - endpoint, ok := inspect.Endpoints["docker"] |
55 | | - if ok { |
56 | | - os.Setenv("DOCKER_HOST", endpoint.Host) |
57 | | - break |
58 | | - } |
59 | | - } |
| 45 | + if clientOptions.host == "" { |
| 46 | + host, err := determineDockerHost() |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("error determining docker host: %w", err) |
60 | 49 | } |
| 50 | + clientOptions.host = host |
61 | 51 | } |
62 | 52 |
|
63 | 53 | // TODO[md]: we create a client at the top of each cli invocation, the sdk client hits an api which |
64 | 54 | // adds (a tiny biy of) overead. swap this with a handle that'll lazily initialize a client and ping for health. |
65 | 55 | // ditto for fetching registry credentials. |
66 | | - client, err := dc.NewClientWithOpts(dc.FromEnv, dc.WithAPIVersionNegotiation()) |
| 56 | + |
| 57 | + dockerClientOpts := []dc.Opt{ |
| 58 | + dc.WithTLSClientConfigFromEnv(), |
| 59 | + dc.WithVersionFromEnv(), |
| 60 | + dc.WithAPIVersionNegotiation(), |
| 61 | + dc.WithHost(clientOptions.host), |
| 62 | + } |
| 63 | + |
| 64 | + client, err := dc.NewClientWithOpts(dockerClientOpts...) |
67 | 65 | if err != nil { |
68 | 66 | return nil, fmt.Errorf("error creating docker client: %w", err) |
69 | 67 | } |
@@ -628,25 +626,3 @@ func shouldAttachStdin(stdin io.Reader) (attach bool, tty bool) { |
628 | 626 | // the container to attach stdin and keep open |
629 | 627 | return true, true |
630 | 628 | } |
631 | | - |
632 | | -func findDockerHost() ([]command.ContextInspect, error) { |
633 | | - dockerCmd := DockerCommandFromEnvironment() |
634 | | - cmd := exec.Command(dockerCmd, "context", "inspect") |
635 | | - |
636 | | - // Create a buffer to capture the standard output |
637 | | - var out bytes.Buffer |
638 | | - cmd.Stdout = &out |
639 | | - |
640 | | - // Run the command |
641 | | - err := cmd.Run() |
642 | | - if err != nil { |
643 | | - return nil, err |
644 | | - } |
645 | | - |
646 | | - var resp []command.ContextInspect |
647 | | - if err := json.Unmarshal(out.Bytes(), &resp); err != nil { |
648 | | - return nil, fmt.Errorf("error unmarshaling inspect response: %w", err) |
649 | | - } |
650 | | - |
651 | | - return resp, nil |
652 | | -} |
0 commit comments