diff --git a/client.go b/client.go index b0a97804..5266fede 100644 --- a/client.go +++ b/client.go @@ -218,10 +218,16 @@ func NewVersionedClient(endpoint string, apiVersionString string) (*Client, erro eventMonitor: new(eventMonitoringState), requestedAPIVersion: requestedAPIVersion, } - c.initializeNativeClient() + c.initializeNativeClient(defaultTransport) return c, nil } +// WithTransport replaces underlying HTTP client of Docker Client by accepting +// a function that returns pointer to a transport object. +func (c *Client) WithTransport(trFunc func () *http.Transport) { + c.initializeNativeClient(trFunc) +} + // NewVersionnedTLSClient has been DEPRECATED, please use NewVersionedTLSClient. func NewVersionnedTLSClient(endpoint string, cert, key, ca, apiVersionString string) (*Client, error) { return NewVersionedTLSClient(endpoint, cert, key, ca, apiVersionString) @@ -339,7 +345,7 @@ func NewVersionedTLSClientFromBytes(endpoint string, certPEMBlock, keyPEMBlock, eventMonitor: new(eventMonitoringState), requestedAPIVersion: requestedAPIVersion, } - c.initializeNativeClient() + c.initializeNativeClient(defaultTransport) return c, nil } diff --git a/client_unix.go b/client_unix.go index ab73cf2b..b1dfe115 100644 --- a/client_unix.go +++ b/client_unix.go @@ -9,21 +9,24 @@ package docker import ( "context" "net" + "net/http" ) // initializeNativeClient initializes the native Unix domain socket client on // Unix-style operating systems -func (c *Client) initializeNativeClient() { +func (c *Client) initializeNativeClient(trFunc func () *http.Transport) { if c.endpointURL.Scheme != unixProtocol { return } - socketPath := c.endpointURL.Path - tr := defaultTransport() + sockPath := c.endpointURL.Path + + tr := trFunc() + tr.Dial = func(network, addr string) (net.Conn, error) { - return c.Dialer.Dial(unixProtocol, socketPath) + return c.Dialer.Dial(unixProtocol, sockPath) } tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { - return c.Dialer.Dial(unixProtocol, socketPath) + return c.Dialer.Dial(unixProtocol, sockPath) } c.HTTPClient.Transport = tr } diff --git a/client_windows.go b/client_windows.go index c9ecc187..c2dda464 100644 --- a/client_windows.go +++ b/client_windows.go @@ -10,6 +10,7 @@ import ( "context" "net" "time" + "net/http" "github.com/Microsoft/go-winio" ) @@ -25,7 +26,7 @@ func (p pipeDialer) Dial(network, address string) (net.Conn, error) { } // initializeNativeClient initializes the native Named Pipe client for Windows -func (c *Client) initializeNativeClient() { +func (c *Client) initializeNativeClient(trFunc func () *http.Transport) { if c.endpointURL.Scheme != namedPipeProtocol { return } @@ -34,7 +35,7 @@ func (c *Client) initializeNativeClient() { timeout := namedPipeConnectTimeout return winio.DialPipe(namedPipePath, &timeout) } - tr := defaultTransport() + tr := trFunc() tr.Dial = dialFunc tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { return dialFunc(network, addr)