Docker go SDK: “client version 1.42 is too new. Maximum supported API version is 1.40”

If you get the error:

panic: Error response from daemon: client version 1.42 is too new. Maximum supported API version is 1.40

For a code like the follow:

In this example, we’ve defined the IPAMConfig struct using the network package.

You should be able to create a Docker network with the specified IPAM configuration using the NetworkCreate function.

package main

import (
	"context"
	"fmt"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/network"
	"github.com/docker/docker/client"
)

func main() {
	cli, err := client.NewEnvClient()
	if err != nil {
		panic(err)
	}

	ipamConfig := &network.IPAMConfig{
		Subnet: "172.18.0.0/16",
	}

	networkResponse, err := cli.NetworkCreate(context.Background(), "kind", types.NetworkCreate{
		CheckDuplicate: true,
		Driver:         "bridge",
		EnableIPv6:     false,
		IPAM: &network.IPAM{
			Driver: "default",
			Config: []network.IPAMConfig{*ipamConfig},
		},
		Internal: false,
	})

	if err != nil {
		panic(err)
	}

	fmt.Printf("Network ID: %s\n", networkResponse.ID)
}

Solution: Add the following to your go.mod

require github.com/docker/docker v23.0.1+incompatible

replace github.com/docker/docker => github.com/docker/docker v17.12.0-ce-rc1.0.20210128214336-420b1d36250f+incompatible

Execute ‘go get .’

It should work now.

Blog at WordPress.com.