@@ -8,8 +8,10 @@ https://docs.docker.com/reference/api/engine/
88
99You use the library by constructing a client object using [NewClientWithOpts]
1010and calling methods on it. The client can be configured from environment
11- variables by passing the [FromEnv] option, or configured manually by passing any
12- of the other available [Opts].
11+ variables by passing the [FromEnv] option, and the [WithAPIVersionNegotiation]
12+ option to allow downgrading the API version used when connecting with an older
13+ daemon version. Other options cen be configured manually by passing any of
14+ the available [Opt] options.
1315
1416For example, to list running containers (the equivalent of "docker ps"):
1517
@@ -18,23 +20,33 @@ For example, to list running containers (the equivalent of "docker ps"):
1820 import (
1921 "context"
2022 "fmt"
23+ "log"
2124
2225 "github.com/moby/moby/client"
2326 )
2427
2528 func main() {
26- cli, err := client.NewClientWithOpts(client.FromEnv)
29+ // Create a new client that handles common environment variables
30+ // for configuration (DOCKER_HOST, DOCKER_API_VERSION), and does
31+ // API-version negotiation to allow downgrading the API version
32+ // when connecting with an older daemon version.
33+ apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
2734 if err != nil {
28- panic (err)
35+ log.Fatal (err)
2936 }
3037
31- containers, err := cli.ContainerList(context.Background(), client.ContainerListOptions{})
38+ // List all containers (both stopped and running).
39+ result, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{
40+ All: true,
41+ })
3242 if err != nil {
33- panic (err)
43+ log.Fatal (err)
3444 }
3545
36- for _, ctr := range containers {
37- fmt.Printf("%s %s\n", ctr.ID, ctr.Image)
46+ // Print each container's ID, status and the image it was created from.
47+ fmt.Printf("%s %-22s %s\n", "ID", "STATUS", "IMAGE")
48+ for _, ctr := range result.Items {
49+ fmt.Printf("%s %-22s %s\n", ctr.ID, ctr.Status, ctr.Image)
3850 }
3951 }
4052*/
0 commit comments