Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -23,19 +23,28 @@ import ( |
| 23 | 23 |
) |
| 24 | 24 |
|
| 25 | 25 |
func main() {
|
| 26 |
+ // Create a new client that handles common environment variables |
|
| 27 |
+ // for configuration (DOCKER_HOST, DOCKER_API_VERSION), and does |
|
| 28 |
+ // API-version negotiation to allow downgrading the API version |
|
| 29 |
+ // when connecting with an older daemon version. |
|
| 26 | 30 |
apiClient, err := client.New(client.FromEnv, client.WithAPIVersionNegotiation()) |
| 27 | 31 |
if err != nil {
|
| 28 | 32 |
panic(err) |
| 29 | 33 |
} |
| 30 | 34 |
defer apiClient.Close() |
| 31 | 35 |
|
| 32 |
- containers, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{All: true})
|
|
| 36 |
+ // List all containers (both stopped and running). |
|
| 37 |
+ result, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{
|
|
| 38 |
+ All: true, |
|
| 39 |
+ }) |
|
| 33 | 40 |
if err != nil {
|
| 34 | 41 |
panic(err) |
| 35 | 42 |
} |
| 36 | 43 |
|
| 37 |
- for _, ctr := range containers {
|
|
| 38 |
- fmt.Printf("%s %s (status: %s)\n", ctr.ID, ctr.Image, ctr.Status)
|
|
| 44 |
+ // Print each container's ID, status and the image it was created from. |
|
| 45 |
+ fmt.Printf("%s %-22s %s\n", "ID", "STATUS", "IMAGE")
|
|
| 46 |
+ for _, ctr := range result.Items {
|
|
| 47 |
+ fmt.Printf("%s %-22s %s\n", ctr.ID, ctr.Status, ctr.Image)
|
|
| 39 | 48 |
} |
| 40 | 49 |
} |
| 41 | 50 |
``` |
| ... | ... |
@@ -23,19 +23,28 @@ import ( |
| 23 | 23 |
) |
| 24 | 24 |
|
| 25 | 25 |
func main() {
|
| 26 |
+ // Create a new client that handles common environment variables |
|
| 27 |
+ // for configuration (DOCKER_HOST, DOCKER_API_VERSION), and does |
|
| 28 |
+ // API-version negotiation to allow downgrading the API version |
|
| 29 |
+ // when connecting with an older daemon version. |
|
| 26 | 30 |
apiClient, err := client.New(client.FromEnv, client.WithAPIVersionNegotiation()) |
| 27 | 31 |
if err != nil {
|
| 28 | 32 |
panic(err) |
| 29 | 33 |
} |
| 30 | 34 |
defer apiClient.Close() |
| 31 | 35 |
|
| 32 |
- containers, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{All: true})
|
|
| 36 |
+ // List all containers (both stopped and running). |
|
| 37 |
+ result, err := apiClient.ContainerList(context.Background(), client.ContainerListOptions{
|
|
| 38 |
+ All: true, |
|
| 39 |
+ }) |
|
| 33 | 40 |
if err != nil {
|
| 34 | 41 |
panic(err) |
| 35 | 42 |
} |
| 36 | 43 |
|
| 37 |
- for _, ctr := range containers {
|
|
| 38 |
- fmt.Printf("%s %s (status: %s)\n", ctr.ID, ctr.Image, ctr.Status)
|
|
| 44 |
+ // Print each container's ID, status and the image it was created from. |
|
| 45 |
+ fmt.Printf("%s %-22s %s\n", "ID", "STATUS", "IMAGE")
|
|
| 46 |
+ for _, ctr := range result.Items {
|
|
| 47 |
+ fmt.Printf("%s %-22s %s\n", ctr.ID, ctr.Status, ctr.Image)
|
|
| 39 | 48 |
} |
| 40 | 49 |
} |
| 41 | 50 |
``` |