2016-10-08 13:34:37 +00:00
|
|
|
/*
|
2016-11-15 19:45:20 +00:00
|
|
|
Package client is a Go client for the Docker Engine API.
|
2016-10-08 13:34:37 +00:00
|
|
|
|
2016-11-15 19:45:20 +00:00
|
|
|
For more information about the Engine API, see the documentation:
|
|
|
|
https://docs.docker.com/engine/reference/api/
|
2016-10-08 13:34:37 +00:00
|
|
|
|
|
|
|
Usage
|
|
|
|
|
|
|
|
You use the library by creating a client object and calling methods on it. The
|
|
|
|
client can be created either from environment variables with NewEnvClient, or
|
|
|
|
configured manually with NewClient.
|
|
|
|
|
|
|
|
For example, to list running containers (the equivalent of "docker ps"):
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cli, err := client.NewEnvClient()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, container := range containers {
|
|
|
|
fmt.Printf("%s %s\n", container.ID[:10], container.Image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"fmt"
|
2018-02-16 18:24:57 +00:00
|
|
|
"net"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2017-08-11 17:47:02 +00:00
|
|
|
"path"
|
2016-09-06 18:46:37 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-02-19 08:43:08 +00:00
|
|
|
"github.com/docker/docker/api"
|
2017-06-21 05:58:16 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-09-09 03:44:25 +00:00
|
|
|
"github.com/docker/go-connections/sockets"
|
2016-09-06 18:46:37 +00:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2018-02-16 18:24:57 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
2017-03-27 09:05:35 +00:00
|
|
|
// ErrRedirect is the error returned by checkRedirect when the request is non-GET.
|
|
|
|
var ErrRedirect = errors.New("unexpected redirect in response")
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
// Client is the API client that performs all operations
|
|
|
|
// against a docker server.
|
|
|
|
type Client struct {
|
2016-10-11 22:53:14 +00:00
|
|
|
// scheme sets the scheme for the client
|
|
|
|
scheme string
|
2016-09-06 18:46:37 +00:00
|
|
|
// host holds the server address to connect to
|
|
|
|
host string
|
|
|
|
// proto holds the client protocol i.e. unix.
|
|
|
|
proto string
|
|
|
|
// addr holds the client address.
|
|
|
|
addr string
|
|
|
|
// basePath holds the path to prepend to the requests.
|
|
|
|
basePath string
|
2016-09-09 03:44:25 +00:00
|
|
|
// client used to send and receive http requests.
|
|
|
|
client *http.Client
|
2016-09-06 18:46:37 +00:00
|
|
|
// version of the server to talk to.
|
|
|
|
version string
|
|
|
|
// custom http headers configured by users.
|
|
|
|
customHTTPHeaders map[string]string
|
2016-11-03 00:43:32 +00:00
|
|
|
// manualOverride is set to true when the version was set by users.
|
|
|
|
manualOverride bool
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 09:05:35 +00:00
|
|
|
// CheckRedirect specifies the policy for dealing with redirect responses:
|
|
|
|
// If the request is non-GET return `ErrRedirect`. Otherwise use the last response.
|
|
|
|
//
|
2017-05-21 23:24:07 +00:00
|
|
|
// Go 1.8 changes behavior for HTTP redirects (specifically 301, 307, and 308) in the client .
|
2017-03-27 09:05:35 +00:00
|
|
|
// The Docker client (and by extension docker API client) can be made to to send a request
|
|
|
|
// like POST /containers//start where what would normally be in the name section of the URL is empty.
|
|
|
|
// This triggers an HTTP 301 from the daemon.
|
|
|
|
// In go 1.8 this 301 will be converted to a GET request, and ends up getting a 404 from the daemon.
|
|
|
|
// This behavior change manifests in the client in that before the 301 was not followed and
|
|
|
|
// the client did not generate an error, but now results in a message like Error response from daemon: page not found.
|
|
|
|
func CheckRedirect(req *http.Request, via []*http.Request) error {
|
|
|
|
if via[0].Method == http.MethodGet {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}
|
|
|
|
return ErrRedirect
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
// NewEnvClient initializes a new API client based on environment variables.
|
2018-02-16 18:24:57 +00:00
|
|
|
// See FromEnv for a list of support environment variables.
|
|
|
|
//
|
|
|
|
// Deprecated: use NewClientWithOpts(FromEnv)
|
2016-09-06 18:46:37 +00:00
|
|
|
func NewEnvClient() (*Client, error) {
|
2018-01-31 19:28:33 +00:00
|
|
|
return NewClientWithOpts(FromEnv)
|
|
|
|
}
|
|
|
|
|
2018-02-16 18:24:57 +00:00
|
|
|
// FromEnv configures the client with values from environment variables.
|
|
|
|
//
|
|
|
|
// Supported environment variables:
|
|
|
|
// DOCKER_HOST to set the url to the docker server.
|
|
|
|
// DOCKER_API_VERSION to set the version of the API to reach, leave empty for latest.
|
|
|
|
// DOCKER_CERT_PATH to load the TLS certificates from.
|
|
|
|
// DOCKER_TLS_VERIFY to enable or disable TLS verification, off by default.
|
2018-01-31 19:28:33 +00:00
|
|
|
func FromEnv(c *Client) error {
|
2016-09-06 18:46:37 +00:00
|
|
|
if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); dockerCertPath != "" {
|
|
|
|
options := tlsconfig.Options{
|
|
|
|
CAFile: filepath.Join(dockerCertPath, "ca.pem"),
|
|
|
|
CertFile: filepath.Join(dockerCertPath, "cert.pem"),
|
|
|
|
KeyFile: filepath.Join(dockerCertPath, "key.pem"),
|
|
|
|
InsecureSkipVerify: os.Getenv("DOCKER_TLS_VERIFY") == "",
|
|
|
|
}
|
|
|
|
tlsc, err := tlsconfig.Client(options)
|
|
|
|
if err != nil {
|
2018-01-31 19:28:33 +00:00
|
|
|
return err
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 18:24:57 +00:00
|
|
|
c.client = &http.Client{
|
|
|
|
Transport: &http.Transport{TLSClientConfig: tlsc},
|
2017-03-27 09:05:35 +00:00
|
|
|
CheckRedirect: CheckRedirect,
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 18:24:57 +00:00
|
|
|
if host := os.Getenv("DOCKER_HOST"); host != "" {
|
2018-01-31 19:28:33 +00:00
|
|
|
if err := WithHost(host)(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2018-02-16 18:24:57 +00:00
|
|
|
|
|
|
|
if version := os.Getenv("DOCKER_API_VERSION"); version != "" {
|
2018-01-31 19:28:33 +00:00
|
|
|
c.version = version
|
|
|
|
c.manualOverride = true
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2018-01-31 19:28:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
|
2018-02-16 18:24:57 +00:00
|
|
|
// WithTLSClientConfig applies a tls config to the client transport.
|
|
|
|
func WithTLSClientConfig(cacertPath, certPath, keyPath string) func(*Client) error {
|
|
|
|
return func(c *Client) error {
|
|
|
|
opts := tlsconfig.Options{
|
|
|
|
CAFile: cacertPath,
|
|
|
|
CertFile: certPath,
|
|
|
|
KeyFile: keyPath,
|
|
|
|
ExclusiveRootPools: true,
|
|
|
|
}
|
|
|
|
config, err := tlsconfig.Client(opts)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create tls config")
|
|
|
|
}
|
|
|
|
if transport, ok := c.client.Transport.(*http.Transport); ok {
|
|
|
|
transport.TLSClientConfig = config
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Errorf("cannot apply tls config to transport: %T", c.client.Transport)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDialer applies the dialer.DialContext to the client transport. This can be
|
|
|
|
// used to set the Timeout and KeepAlive settings of the client.
|
2018-03-19 08:33:06 +00:00
|
|
|
// Deprecated: use WithDialContext
|
2018-02-16 18:24:57 +00:00
|
|
|
func WithDialer(dialer *net.Dialer) func(*Client) error {
|
2018-03-19 08:33:06 +00:00
|
|
|
return WithDialContext(dialer.DialContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDialContext applies the dialer to the client transport. This can be
|
|
|
|
// used to set the Timeout and KeepAlive settings of the client.
|
|
|
|
func WithDialContext(dialContext func(ctx context.Context, network, addr string) (net.Conn, error)) func(*Client) error {
|
2018-02-16 18:24:57 +00:00
|
|
|
return func(c *Client) error {
|
|
|
|
if transport, ok := c.client.Transport.(*http.Transport); ok {
|
2018-03-19 08:33:06 +00:00
|
|
|
transport.DialContext = dialContext
|
2018-02-16 18:24:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Errorf("cannot apply dialer to transport: %T", c.client.Transport)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:28:33 +00:00
|
|
|
// WithVersion overrides the client version with the specified one
|
|
|
|
func WithVersion(version string) func(*Client) error {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.version = version
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 18:24:57 +00:00
|
|
|
// WithHost overrides the client host with the specified one.
|
2018-01-31 19:28:33 +00:00
|
|
|
func WithHost(host string) func(*Client) error {
|
|
|
|
return func(c *Client) error {
|
|
|
|
hostURL, err := ParseHostURL(host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.host = host
|
|
|
|
c.proto = hostURL.Scheme
|
|
|
|
c.addr = hostURL.Host
|
|
|
|
c.basePath = hostURL.Path
|
2018-02-09 23:10:58 +00:00
|
|
|
if transport, ok := c.client.Transport.(*http.Transport); ok {
|
|
|
|
return sockets.ConfigureTransport(transport, c.proto, c.addr)
|
2018-01-31 19:28:33 +00:00
|
|
|
}
|
2018-02-16 18:24:57 +00:00
|
|
|
return errors.Errorf("cannot apply host to transport: %T", c.client.Transport)
|
2016-11-03 00:43:32 +00:00
|
|
|
}
|
2018-01-31 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithHTTPClient overrides the client http client with the specified one
|
|
|
|
func WithHTTPClient(client *http.Client) func(*Client) error {
|
|
|
|
return func(c *Client) error {
|
|
|
|
if client != nil {
|
|
|
|
c.client = client
|
|
|
|
}
|
|
|
|
return nil
|
2016-11-03 00:43:32 +00:00
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 19:28:33 +00:00
|
|
|
// WithHTTPHeaders overrides the client default http headers
|
|
|
|
func WithHTTPHeaders(headers map[string]string) func(*Client) error {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.customHTTPHeaders = headers
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 17:40:37 +00:00
|
|
|
// WithScheme overrides the client scheme with the specified one
|
|
|
|
func WithScheme(scheme string) func(*Client) error {
|
|
|
|
return func(c *Client) error {
|
|
|
|
c.scheme = scheme
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:28:33 +00:00
|
|
|
// NewClientWithOpts initializes a new API client with default values. It takes functors
|
|
|
|
// to modify values when creating it, like `NewClientWithOpts(WithVersion(…))`
|
2016-09-06 18:46:37 +00:00
|
|
|
// It also initializes the custom http headers to add to each request.
|
|
|
|
//
|
|
|
|
// It won't send any version information if the version number is empty. It is
|
|
|
|
// highly recommended that you set a version or your client may break if the
|
|
|
|
// server is upgraded.
|
2018-01-31 19:28:33 +00:00
|
|
|
func NewClientWithOpts(ops ...func(*Client) error) (*Client, error) {
|
|
|
|
client, err := defaultHTTPClient(DefaultDockerHost)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-31 19:28:33 +00:00
|
|
|
c := &Client{
|
|
|
|
host: DefaultDockerHost,
|
|
|
|
version: api.DefaultVersion,
|
|
|
|
client: client,
|
|
|
|
proto: defaultProto,
|
|
|
|
addr: defaultAddr,
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
|
2018-01-31 19:28:33 +00:00
|
|
|
for _, op := range ops {
|
|
|
|
if err := op(c); err != nil {
|
|
|
|
return nil, err
|
2016-09-22 02:16:44 +00:00
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 19:28:33 +00:00
|
|
|
if _, ok := c.client.Transport.(http.RoundTripper); !ok {
|
|
|
|
return nil, fmt.Errorf("unable to verify TLS configuration, invalid transport %v", c.client.Transport)
|
|
|
|
}
|
2018-11-01 17:40:37 +00:00
|
|
|
if c.scheme == "" {
|
|
|
|
c.scheme = "http"
|
|
|
|
|
|
|
|
tlsConfig := resolveTLSConfig(c.client.Transport)
|
|
|
|
if tlsConfig != nil {
|
|
|
|
// TODO(stevvooe): This isn't really the right way to write clients in Go.
|
|
|
|
// `NewClient` should probably only take an `*http.Client` and work from there.
|
|
|
|
// Unfortunately, the model of having a host-ish/url-thingy as the connection
|
|
|
|
// string has us confusing protocol and transport layers. We continue doing
|
|
|
|
// this to avoid breaking existing clients but this should be addressed.
|
|
|
|
c.scheme = "https"
|
|
|
|
}
|
2018-01-31 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultHTTPClient(host string) (*http.Client, error) {
|
|
|
|
url, err := ParseHostURL(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
transport := new(http.Transport)
|
|
|
|
sockets.ConfigureTransport(transport, url.Scheme, url.Host)
|
|
|
|
return &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
CheckRedirect: CheckRedirect,
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-31 19:28:33 +00:00
|
|
|
// NewClient initializes a new API client for the given host and API version.
|
|
|
|
// It uses the given http client as transport.
|
|
|
|
// It also initializes the custom http headers to add to each request.
|
|
|
|
//
|
|
|
|
// It won't send any version information if the version number is empty. It is
|
|
|
|
// highly recommended that you set a version or your client may break if the
|
|
|
|
// server is upgraded.
|
2018-02-16 18:24:57 +00:00
|
|
|
// Deprecated: use NewClientWithOpts
|
2018-01-31 19:28:33 +00:00
|
|
|
func NewClient(host string, version string, client *http.Client, httpHeaders map[string]string) (*Client, error) {
|
|
|
|
return NewClientWithOpts(WithHost(host), WithVersion(version), WithHTTPClient(client), WithHTTPHeaders(httpHeaders))
|
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// Close the transport used by the client
|
2016-09-08 01:57:54 +00:00
|
|
|
func (cli *Client) Close() error {
|
|
|
|
if t, ok := cli.client.Transport.(*http.Transport); ok {
|
|
|
|
t.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
// getAPIPath returns the versioned request path to call the api.
|
|
|
|
// It appends the query parameters to the path if they are not empty.
|
|
|
|
func (cli *Client) getAPIPath(p string, query url.Values) string {
|
|
|
|
var apiPath string
|
|
|
|
if cli.version != "" {
|
|
|
|
v := strings.TrimPrefix(cli.version, "v")
|
2017-09-07 17:46:23 +00:00
|
|
|
apiPath = path.Join(cli.basePath, "/v"+v, p)
|
2016-09-06 18:46:37 +00:00
|
|
|
} else {
|
2017-08-11 17:47:02 +00:00
|
|
|
apiPath = path.Join(cli.basePath, p)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2017-09-07 17:46:23 +00:00
|
|
|
return (&url.URL{Path: apiPath, RawQuery: query.Encode()}).String()
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// ClientVersion returns the API version used by this client.
|
2016-09-06 18:46:37 +00:00
|
|
|
func (cli *Client) ClientVersion() string {
|
|
|
|
return cli.version
|
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// NegotiateAPIVersion queries the API and updates the version to match the
|
|
|
|
// API version. Any errors are silently ignored.
|
2017-06-21 05:58:16 +00:00
|
|
|
func (cli *Client) NegotiateAPIVersion(ctx context.Context) {
|
|
|
|
ping, _ := cli.Ping(ctx)
|
|
|
|
cli.NegotiateAPIVersionPing(ping)
|
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// NegotiateAPIVersionPing updates the client version to match the Ping.APIVersion
|
|
|
|
// if the ping version is less than the default version.
|
2017-06-21 05:58:16 +00:00
|
|
|
func (cli *Client) NegotiateAPIVersionPing(p types.Ping) {
|
|
|
|
if cli.manualOverride {
|
|
|
|
return
|
2016-11-03 00:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 05:58:16 +00:00
|
|
|
// try the latest version before versioning headers existed
|
|
|
|
if p.APIVersion == "" {
|
|
|
|
p.APIVersion = "1.24"
|
|
|
|
}
|
|
|
|
|
2017-06-30 05:24:49 +00:00
|
|
|
// if the client is not initialized with a version, start with the latest supported version
|
|
|
|
if cli.version == "" {
|
|
|
|
cli.version = api.DefaultVersion
|
|
|
|
}
|
|
|
|
|
2017-09-28 01:12:13 +00:00
|
|
|
// if server version is lower than the client version, downgrade
|
|
|
|
if versions.LessThan(p.APIVersion, cli.version) {
|
2017-06-21 05:58:16 +00:00
|
|
|
cli.version = p.APIVersion
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// DaemonHost returns the host address used by the client
|
2017-05-17 14:46:15 +00:00
|
|
|
func (cli *Client) DaemonHost() string {
|
|
|
|
return cli.host
|
|
|
|
}
|
|
|
|
|
2018-05-15 13:12:30 +00:00
|
|
|
// HTTPClient returns a copy of the HTTP client bound to the server
|
|
|
|
func (cli *Client) HTTPClient() *http.Client {
|
|
|
|
return &*cli.client
|
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// ParseHostURL parses a url string, validates the string is a host url, and
|
|
|
|
// returns the parsed URL
|
|
|
|
func ParseHostURL(host string) (*url.URL, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
protoAddrParts := strings.SplitN(host, "://", 2)
|
|
|
|
if len(protoAddrParts) == 1 {
|
2017-09-07 16:22:11 +00:00
|
|
|
return nil, fmt.Errorf("unable to parse docker host `%s`", host)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var basePath string
|
|
|
|
proto, addr := protoAddrParts[0], protoAddrParts[1]
|
|
|
|
if proto == "tcp" {
|
|
|
|
parsed, err := url.Parse("tcp://" + addr)
|
|
|
|
if err != nil {
|
2017-09-07 16:22:11 +00:00
|
|
|
return nil, err
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
addr = parsed.Host
|
|
|
|
basePath = parsed.Path
|
|
|
|
}
|
2017-09-07 16:22:11 +00:00
|
|
|
return &url.URL{
|
|
|
|
Scheme: proto,
|
|
|
|
Host: addr,
|
|
|
|
Path: basePath,
|
|
|
|
}, nil
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
2016-10-21 05:41:54 +00:00
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// CustomHTTPHeaders returns the custom http headers stored by the client.
|
2016-10-21 05:41:54 +00:00
|
|
|
func (cli *Client) CustomHTTPHeaders() map[string]string {
|
|
|
|
m := make(map[string]string)
|
|
|
|
for k, v := range cli.customHTTPHeaders {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// SetCustomHTTPHeaders that will be set on every HTTP request made by the client.
|
2018-02-16 18:24:57 +00:00
|
|
|
// Deprecated: use WithHTTPHeaders when creating the client.
|
2016-10-21 05:41:54 +00:00
|
|
|
func (cli *Client) SetCustomHTTPHeaders(headers map[string]string) {
|
|
|
|
cli.customHTTPHeaders = headers
|
|
|
|
}
|
2018-03-19 08:33:06 +00:00
|
|
|
|
|
|
|
// Dialer returns a dialer for a raw stream connection, with HTTP/1.1 header, that can be used for proxying the daemon connection.
|
|
|
|
// Used by `docker dial-stdio` (docker/cli#889).
|
|
|
|
func (cli *Client) Dialer() func(context.Context) (net.Conn, error) {
|
|
|
|
return func(ctx context.Context) (net.Conn, error) {
|
|
|
|
if transport, ok := cli.client.Transport.(*http.Transport); ok {
|
2018-09-04 22:09:44 +00:00
|
|
|
if transport.DialContext != nil && transport.TLSClientConfig == nil {
|
2018-03-19 08:33:06 +00:00
|
|
|
return transport.DialContext(ctx, cli.proto, cli.addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fallbackDial(cli.proto, cli.addr, resolveTLSConfig(cli.client.Transport))
|
|
|
|
}
|
|
|
|
}
|