From 2729703967ed3be7d62d9828acfa9bf331c70ef8 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 12 Nov 2025 17:18:09 +0100 Subject: [PATCH] client: fix example in README (align with ExampleNew()) Signed-off-by: Sebastiaan van Stijn --- client/README.md | 15 ++++++++++++--- client/client_example_test.go | 1 + vendor/github.com/moby/moby/client/README.md | 15 ++++++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/client/README.md b/client/README.md index c8c5b96f92..803567a2e2 100644 --- a/client/README.md +++ b/client/README.md @@ -23,19 +23,28 @@ import ( ) func main() { + // Create a new client that handles common environment variables + // for configuration (DOCKER_HOST, DOCKER_API_VERSION), and does + // API-version negotiation to allow downgrading the API version + // when connecting with an older daemon version. apiClient, err := client.New(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { panic(err) } defer apiClient.Close() - containers, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{All: true}) + // List all containers (both stopped and running). + result, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{ + All: true, + }) if err != nil { panic(err) } - for _, ctr := range containers { - fmt.Printf("%s %s (status: %s)\n", ctr.ID, ctr.Image, ctr.Status) + // Print each container's ID, status and the image it was created from. + fmt.Printf("%s %-22s %s\n", "ID", "STATUS", "IMAGE") + for _, ctr := range result.Items { + fmt.Printf("%s %-22s %s\n", ctr.ID, ctr.Status, ctr.Image) } } ``` diff --git a/client/client_example_test.go b/client/client_example_test.go index cfca541f72..b453ee860e 100644 --- a/client/client_example_test.go +++ b/client/client_example_test.go @@ -17,6 +17,7 @@ func ExampleNew() { if err != nil { log.Fatal(err) } + defer apiClient.Close() // List all containers (both stopped and running). result, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{ diff --git a/vendor/github.com/moby/moby/client/README.md b/vendor/github.com/moby/moby/client/README.md index c8c5b96f92..803567a2e2 100644 --- a/vendor/github.com/moby/moby/client/README.md +++ b/vendor/github.com/moby/moby/client/README.md @@ -23,19 +23,28 @@ import ( ) func main() { + // Create a new client that handles common environment variables + // for configuration (DOCKER_HOST, DOCKER_API_VERSION), and does + // API-version negotiation to allow downgrading the API version + // when connecting with an older daemon version. apiClient, err := client.New(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { panic(err) } defer apiClient.Close() - containers, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{All: true}) + // List all containers (both stopped and running). + result, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{ + All: true, + }) if err != nil { panic(err) } - for _, ctr := range containers { - fmt.Printf("%s %s (status: %s)\n", ctr.ID, ctr.Image, ctr.Status) + // Print each container's ID, status and the image it was created from. + fmt.Printf("%s %-22s %s\n", "ID", "STATUS", "IMAGE") + for _, ctr := range result.Items { + fmt.Printf("%s %-22s %s\n", ctr.ID, ctr.Status, ctr.Image) } } ```