Skip to content

Commit 7a8d873

Browse files
authored
Merge pull request moby#51330 from thaJeztah/fix_client_example
client: fix example, and add runnable example
2 parents ef17deb + 3c62b06 commit 7a8d873

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

client/client.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ https://docs.docker.com/reference/api/engine/
88
99
You use the library by constructing a client object using [NewClientWithOpts]
1010
and 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
1416
For 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
*/

client/client_example_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package client_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/moby/moby/client"
9+
)
10+
11+
func ExampleNewClientWithOpts() {
12+
// Create a new client that handles common environment variables
13+
// for configuration (DOCKER_HOST, DOCKER_API_VERSION), and does
14+
// API-version negotiation to allow downgrading the API version
15+
// when connecting with an older daemon version.
16+
apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
17+
if err != nil {
18+
log.Fatal(err)
19+
}
20+
21+
// List all containers (both stopped and running).
22+
result, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{
23+
All: true,
24+
})
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
29+
// Print each container's ID, status and the image it was created from.
30+
fmt.Printf("%s %-22s %s\n", "ID", "STATUS", "IMAGE")
31+
for _, ctr := range result.Items {
32+
fmt.Printf("%s %-22s %s\n", ctr.ID, ctr.Status, ctr.Image)
33+
}
34+
}

vendor/github.com/moby/moby/client/client.go

Lines changed: 20 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)