import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/client" ) func main() { cli, err := client.NewClientWithOpts(client.FromEnv) if err != nil { panic(err) } containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{}) if err != nil { panic(err) } for _, container := range containers { fmt.Printf("%s %s\n", container.ID[:10], container.Image) } }
import ( "context" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" "io" "os" ) func main() { cli, err := client.NewClientWithOpts(client.FromEnv) if err != nil { panic(err) } ctx := context.Background() reader, err := cli.ImagePull(ctx, "ubuntu", types.ImagePullOptions{}) if err != nil { panic(err) } io.Copy(os.Stdout, reader) resp, err := cli.ContainerCreate(ctx, &container.Config{ Image: "ubuntu", Cmd: []string{"echo", "hello world"}, Tty: true, }, nil, nil, "") if err != nil { panic(err) } if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { panic(err) } }This example showcases how to start a new Docker container using the DockerCli Client. It uses the client.ImagePull() function to download the "ubuntu" image, and cli.ContainerCreate() function to create a new container with specified configuration. The container is started using cli.ContainerStart() function. Note: The examples assume that Docker is installed and running on the machine. Additionally, authentication and access credentials may be required for some Docker commands.