client.go 10 KB

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