When building things that will interact with the client/server you can have a mismatch of API versions.
This is a common way to get started with the Docker client in Go: (this will create a new client based on your environment settings for Docker)
cli, err := client.NewClientWithOpts(client.FromEnv)
You may face an error similar to:
Error response from daemon: client version 1.40 is too new. Maximum supported API version is 1.39
One solution is to let the client
discover what's the appropriated version to use:
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv)
cli.NegotiateAPIVersion(ctx)
Full example:
package main
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv)
cli.NegotiateAPIVersion(ctx)
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)
}
}