github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/client/client.go (about) 1 /* 2 Package client is a Go client for the Docker Engine API. 3 4 For more information about the Engine API, see the documentation: 5 https://docs.docker.com/engine/api/ 6 7 # Usage 8 9 You use the library by creating a client object and calling methods on it. The 10 client can be created either from environment variables with NewClientWithOpts(client.FromEnv), 11 or configured manually with NewClient(). 12 13 For example, to list running containers (the equivalent of "docker ps"): 14 15 package main 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/docker/docker/api/types" 22 "github.com/docker/docker/client" 23 ) 24 25 func main() { 26 cli, err := client.NewClientWithOpts(client.FromEnv) 27 if err != nil { 28 panic(err) 29 } 30 31 containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{}) 32 if err != nil { 33 panic(err) 34 } 35 36 for _, container := range containers { 37 fmt.Printf("%s %s\n", container.ID[:10], container.Image) 38 } 39 } 40 */ 41 package client // import "github.com/docker/docker/client" 42 43 import ( 44 "context" 45 "net" 46 "net/http" 47 "net/url" 48 "path" 49 "strings" 50 51 "github.com/docker/docker/api" 52 "github.com/docker/docker/api/types" 53 "github.com/docker/docker/api/types/versions" 54 "github.com/docker/go-connections/sockets" 55 "github.com/pkg/errors" 56 ) 57 58 // ErrRedirect is the error returned by checkRedirect when the request is non-GET. 59 var ErrRedirect = errors.New("unexpected redirect in response") 60 61 // Client is the API client that performs all operations 62 // against a docker server. 63 type Client struct { 64 // scheme sets the scheme for the client 65 scheme string 66 // host holds the server address to connect to 67 host string 68 // proto holds the client protocol i.e. unix. 69 proto string 70 // addr holds the client address. 71 addr string 72 // basePath holds the path to prepend to the requests. 73 basePath string 74 // client used to send and receive http requests. 75 client *http.Client 76 // version of the server to talk to. 77 version string 78 // custom http headers configured by users. 79 customHTTPHeaders map[string]string 80 // manualOverride is set to true when the version was set by users. 81 manualOverride bool 82 83 // negotiateVersion indicates if the client should automatically negotiate 84 // the API version to use when making requests. API version negotiation is 85 // performed on the first request, after which negotiated is set to "true" 86 // so that subsequent requests do not re-negotiate. 87 negotiateVersion bool 88 89 // negotiated indicates that API version negotiation took place 90 negotiated bool 91 } 92 93 // CheckRedirect specifies the policy for dealing with redirect responses: 94 // If the request is non-GET return ErrRedirect, otherwise use the last response. 95 // 96 // Go 1.8 changes behavior for HTTP redirects (specifically 301, 307, and 308) 97 // in the client. The Docker client (and by extension docker API client) can be 98 // made to send a request like POST /containers//start where what would normally 99 // be in the name section of the URL is empty. This triggers an HTTP 301 from 100 // the daemon. 101 // 102 // In go 1.8 this 301 will be converted to a GET request, and ends up getting 103 // a 404 from the daemon. This behavior change manifests in the client in that 104 // before, the 301 was not followed and the client did not generate an error, 105 // but now results in a message like Error response from daemon: page not found. 106 func CheckRedirect(req *http.Request, via []*http.Request) error { 107 if via[0].Method == http.MethodGet { 108 return http.ErrUseLastResponse 109 } 110 return ErrRedirect 111 } 112 113 // NewClientWithOpts initializes a new API client with a default HTTPClient, and 114 // default API host and version. It also initializes the custom HTTP headers to 115 // add to each request. 116 // 117 // It takes an optional list of Opt functional arguments, which are applied in 118 // the order they're provided, which allows modifying the defaults when creating 119 // the client. For example, the following initializes a client that configures 120 // itself with values from environment variables (client.FromEnv), and has 121 // automatic API version negotiation enabled (client.WithAPIVersionNegotiation()). 122 // 123 // cli, err := client.NewClientWithOpts( 124 // client.FromEnv, 125 // client.WithAPIVersionNegotiation(), 126 // ) 127 func NewClientWithOpts(ops ...Opt) (*Client, error) { 128 client, err := defaultHTTPClient(DefaultDockerHost) 129 if err != nil { 130 return nil, err 131 } 132 c := &Client{ 133 host: DefaultDockerHost, 134 version: api.DefaultVersion, 135 client: client, 136 proto: defaultProto, 137 addr: defaultAddr, 138 } 139 140 for _, op := range ops { 141 if err := op(c); err != nil { 142 return nil, err 143 } 144 } 145 146 if c.scheme == "" { 147 c.scheme = "http" 148 149 tlsConfig := resolveTLSConfig(c.client.Transport) 150 if tlsConfig != nil { 151 // TODO(stevvooe): This isn't really the right way to write clients in Go. 152 // `NewClient` should probably only take an `*http.Client` and work from there. 153 // Unfortunately, the model of having a host-ish/url-thingy as the connection 154 // string has us confusing protocol and transport layers. We continue doing 155 // this to avoid breaking existing clients but this should be addressed. 156 c.scheme = "https" 157 } 158 } 159 160 return c, nil 161 } 162 163 func defaultHTTPClient(host string) (*http.Client, error) { 164 hostURL, err := ParseHostURL(host) 165 if err != nil { 166 return nil, err 167 } 168 transport := &http.Transport{} 169 _ = sockets.ConfigureTransport(transport, hostURL.Scheme, hostURL.Host) 170 return &http.Client{ 171 Transport: transport, 172 CheckRedirect: CheckRedirect, 173 }, nil 174 } 175 176 // Close the transport used by the client 177 func (cli *Client) Close() error { 178 if t, ok := cli.client.Transport.(*http.Transport); ok { 179 t.CloseIdleConnections() 180 } 181 return nil 182 } 183 184 // getAPIPath returns the versioned request path to call the api. 185 // It appends the query parameters to the path if they are not empty. 186 func (cli *Client) getAPIPath(ctx context.Context, p string, query url.Values) string { 187 var apiPath string 188 if cli.negotiateVersion && !cli.negotiated { 189 cli.NegotiateAPIVersion(ctx) 190 } 191 if cli.version != "" { 192 v := strings.TrimPrefix(cli.version, "v") 193 apiPath = path.Join(cli.basePath, "/v"+v, p) 194 } else { 195 apiPath = path.Join(cli.basePath, p) 196 } 197 return (&url.URL{Path: apiPath, RawQuery: query.Encode()}).String() 198 } 199 200 // ClientVersion returns the API version used by this client. 201 func (cli *Client) ClientVersion() string { 202 return cli.version 203 } 204 205 // NegotiateAPIVersion queries the API and updates the version to match the API 206 // version. NegotiateAPIVersion downgrades the client's API version to match the 207 // APIVersion if the ping version is lower than the default version. If the API 208 // version reported by the server is higher than the maximum version supported 209 // by the client, it uses the client's maximum version. 210 // 211 // If a manual override is in place, either through the "DOCKER_API_VERSION" 212 // (EnvOverrideAPIVersion) environment variable, or if the client is initialized 213 // with a fixed version (WithVersion(xx)), no negotiation is performed. 214 // 215 // If the API server's ping response does not contain an API version, or if the 216 // client did not get a successful ping response, it assumes it is connected with 217 // an old daemon that does not support API version negotiation, in which case it 218 // downgrades to the latest version of the API before version negotiation was 219 // added (1.24). 220 func (cli *Client) NegotiateAPIVersion(ctx context.Context) { 221 if !cli.manualOverride { 222 ping, _ := cli.Ping(ctx) 223 cli.negotiateAPIVersionPing(ping) 224 } 225 } 226 227 // NegotiateAPIVersionPing downgrades the client's API version to match the 228 // APIVersion in the ping response. If the API version in pingResponse is higher 229 // than the maximum version supported by the client, it uses the client's maximum 230 // version. 231 // 232 // If a manual override is in place, either through the "DOCKER_API_VERSION" 233 // (EnvOverrideAPIVersion) environment variable, or if the client is initialized 234 // with a fixed version (WithVersion(xx)), no negotiation is performed. 235 // 236 // If the API server's ping response does not contain an API version, we assume 237 // we are connected with an old daemon without API version negotiation support, 238 // and downgrade to the latest version of the API before version negotiation was 239 // added (1.24). 240 func (cli *Client) NegotiateAPIVersionPing(pingResponse types.Ping) { 241 if !cli.manualOverride { 242 cli.negotiateAPIVersionPing(pingResponse) 243 } 244 } 245 246 // negotiateAPIVersionPing queries the API and updates the version to match the 247 // API version from the ping response. 248 func (cli *Client) negotiateAPIVersionPing(pingResponse types.Ping) { 249 // default to the latest version before versioning headers existed 250 if pingResponse.APIVersion == "" { 251 pingResponse.APIVersion = "1.24" 252 } 253 254 // if the client is not initialized with a version, start with the latest supported version 255 if cli.version == "" { 256 cli.version = api.DefaultVersion 257 } 258 259 // if server version is lower than the client version, downgrade 260 if versions.LessThan(pingResponse.APIVersion, cli.version) { 261 cli.version = pingResponse.APIVersion 262 } 263 264 // Store the results, so that automatic API version negotiation (if enabled) 265 // won't be performed on the next request. 266 if cli.negotiateVersion { 267 cli.negotiated = true 268 } 269 } 270 271 // DaemonHost returns the host address used by the client 272 func (cli *Client) DaemonHost() string { 273 return cli.host 274 } 275 276 // HTTPClient returns a copy of the HTTP client bound to the server 277 func (cli *Client) HTTPClient() *http.Client { 278 c := *cli.client 279 return &c 280 } 281 282 // ParseHostURL parses a url string, validates the string is a host url, and 283 // returns the parsed URL 284 func ParseHostURL(host string) (*url.URL, error) { 285 protoAddrParts := strings.SplitN(host, "://", 2) 286 if len(protoAddrParts) == 1 { 287 return nil, errors.Errorf("unable to parse docker host `%s`", host) 288 } 289 290 var basePath string 291 proto, addr := protoAddrParts[0], protoAddrParts[1] 292 if proto == "tcp" { 293 parsed, err := url.Parse("tcp://" + addr) 294 if err != nil { 295 return nil, err 296 } 297 addr = parsed.Host 298 basePath = parsed.Path 299 } 300 return &url.URL{ 301 Scheme: proto, 302 Host: addr, 303 Path: basePath, 304 }, nil 305 } 306 307 // Dialer returns a dialer for a raw stream connection, with an HTTP/1.1 header, 308 // that can be used for proxying the daemon connection. 309 // 310 // Used by `docker dial-stdio` (docker/cli#889). 311 func (cli *Client) Dialer() func(context.Context) (net.Conn, error) { 312 return func(ctx context.Context) (net.Conn, error) { 313 if transport, ok := cli.client.Transport.(*http.Transport); ok { 314 if transport.DialContext != nil && transport.TLSClientConfig == nil { 315 return transport.DialContext(ctx, cli.proto, cli.addr) 316 } 317 } 318 return fallbackDial(cli.proto, cli.addr, resolveTLSConfig(cli.client.Transport)) 319 } 320 }