-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
42 lines (38 loc) · 1.05 KB
/
client.go
File metadata and controls
42 lines (38 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package dockergo
import (
"context"
"net"
"net/http"
"time"
)
// Client is the SDK entry point.
type Client struct {
HTTP *http.Client
SocketPath string // e.g. /var/run/docker.sock
HostURL string // e.g. http://localhost (used as base for requests)
timeout time.Duration
}
// NewClient returns a client connected to the local Docker UNIX socket.
// If socketPath is empty, defaults to /var/run/docker.sock.
func NewClient(socketPath string) *Client {
if socketPath == "" {
socketPath = "/var/run/docker.sock"
}
transport := &http.Transport{
// DialContext to use unix domain socket
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
d := net.Dialer{Timeout: 5 * time.Second}
return d.DialContext(ctx, "unix", socketPath)
},
}
httpClient := &http.Client{
Transport: transport,
Timeout: 15 * time.Second,
}
return &Client{
HTTP: httpClient,
SocketPath: socketPath,
HostURL: "http://localhost", // used as host in request URL (socket transport ignores host)
timeout: 15 * time.Second,
}
}