import ( "context" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types" "github.com/docker/docker/api/client" ) func main() { cli, err := client.NewEnvClient() if err != nil { panic(err) } ctx := context.Background() resp, err := cli.ContainerCreate(ctx, &container.Config{ Image: "nginx", ExposedPorts: nat.PortSet{ "80/tcp": struct{}{}, }, }, &container.HostConfig{ Mounts: []mount.Mount{ { Type: mount.TypeBind, Source: "/data", Target: "/data", }, }, }, &network.NetworkingConfig{}, "") if err != nil { panic(err) } if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { panic(err) } }
import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/api/client" ) func main() { cli, err := client.NewEnvClient() if err != nil { panic(err) } ctx := context.Background() containers, err := cli.ContainerList(ctx, types.ContainerListOptions{}) if err != nil { panic(err) } for _, container := range containers { fmt.Println(container.ID) } }This code snippet lists all existing containers on the Docker daemon using `ContainerList()` method and prints their ID to console.