package main import ( "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/client" "context" ) func main() { cli, err := client.NewEnvClient() if err != nil { panic(err) } ctx := context.Background() listFilters := filters.NewArgs() listFilters.Add("status", "running") containers, err := cli.ContainerList(ctx, types.ContainerListOptions{ Filters: listFilters, }) if err != nil { panic(err) } for _, container := range containers { fmt.Println(container.Names[0], container.Image) } }
package main import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/client" "io" "os" ) func main() { cli, err := client.NewEnvClient() if err != nil { panic(err) } ctx := context.Background() imageRef := "nginx:latest" reader, err := cli.ImagePull(ctx, imageRef, types.ImagePullOptions{}) if err != nil { panic(err) } defer reader.Close() io.Copy(os.Stdout, reader) fmt.Println(imageRef, "pulled successfully") }This example uses the ImagePull method to pull a Docker image from a registry. It connects to the Docker daemon and pulls the specified image (in this case, the nginx:latest image). Overall, the DockerCli package provides a powerful set of tools for building Docker applications in Go. With it, you can easily interact with a Docker host and execute a variety of commands.