2016-11-15 19:45:20 +00:00
|
|
|
|
# Go client for the Docker Engine API
|
2016-09-12 09:41:11 +00:00
|
|
|
|
|
2023-11-14 11:27:35 +00:00
|
|
|
|
The `docker` command uses this package to communicate with the daemon. It can
|
|
|
|
|
also be used by your own Go applications to do anything the command-line
|
|
|
|
|
interface does – running containers, pulling images, managing swarms, etc.
|
2016-09-12 09:41:11 +00:00
|
|
|
|
|
2023-11-14 11:27:35 +00:00
|
|
|
|
For example, to list all containers (the equivalent of `docker ps --all`):
|
2016-09-12 09:41:11 +00:00
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2016-09-22 07:00:30 +00:00
|
|
|
|
"context"
|
2016-10-08 13:34:37 +00:00
|
|
|
|
"fmt"
|
2016-09-12 09:41:11 +00:00
|
|
|
|
|
2023-11-14 11:27:35 +00:00
|
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-10-08 13:34:37 +00:00
|
|
|
|
"github.com/docker/docker/client"
|
2016-09-12 09:41:11 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2023-11-14 11:27:35 +00:00
|
|
|
|
apiClient, err := client.NewClientWithOpts(client.FromEnv)
|
2016-09-12 09:41:11 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2023-11-14 11:27:35 +00:00
|
|
|
|
defer apiClient.Close()
|
2016-09-12 09:41:11 +00:00
|
|
|
|
|
2023-11-14 11:27:35 +00:00
|
|
|
|
containers, err := apiClient.ContainerList(context.Background(), container.ListOptions{All: true})
|
2016-09-12 09:41:11 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 11:27:35 +00:00
|
|
|
|
for _, ctr := range containers {
|
|
|
|
|
fmt.Printf("%s %s (status: %s)\n", ctr.ID, ctr.Image, ctr.Status)
|
2016-09-12 09:41:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2016-10-08 13:34:37 +00:00
|
|
|
|
|
2023-11-14 11:27:35 +00:00
|
|
|
|
[Full documentation is available on pkg.go.dev.](https://pkg.go.dev/github.com/docker/docker/client)
|