How to Use the Docker SDK in Go

Use the third-party github.com/docker/docker/client package to interact with Docker from Go code.

The Go standard library does not include a Docker SDK; you must use the third-party github.com/docker/docker/client package to interact with Docker from Go code.

package main

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)
	}
	ctx := context.Background()
	info, err := cli.Info(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Docker version: %s\n", info.ServerVersion)
}