moby: Undefined error for `client.NewClientWithOpts` in Go SDK

Description Since the SDK documentation is a bit outdated I’ve no idea what version of SDK I should use and what are the correct methods to call that are not deprecated for creating a new client.

Am I using an incorrect version or have something cached on my machine which results in the errors mentioned below? I think it’s probably because of a cache issue which I get these errors.

Steps to reproduce the issue:

  1. Import github.com/docker/docker/client
  2. Call client.NewClientWithOpts()
  3. Get undefined: client.NewClientWithOpts error

Describe the results you received: Here are the errors I got after running my code:

./main.go:107:14: undefined: client.NewClientWithOpts
./main.go:107:39: undefined: client.WithVersion
./main.go:305:40: too many arguments in call to d.cli.ContainerWait
./main.go:305:70: undefined: container.WaitConditionNotRunning
./main.go:307:14: invalid operation: <-errCh (receive from non-chan type error)
./main.go:311:17: invalid operation: <-statusCh (receive from non-chan type int64)

Describe the results you expected: Client getting created successfully and waiting for the container to exit without any errors.

Additional information you deem important (e.g. issue happens only occasionally): Here is my go.mod file:

module go.txtdirect.org/txtdirect/e2e

go 1.13

require (
	github.com/Microsoft/go-winio v0.4.14 // indirect
	github.com/docker/distribution v2.7.1+incompatible // indirect
	github.com/docker/docker v1.13.1-rc2
	github.com/docker/go-connections v0.4.0
	github.com/docker/go-units v0.4.0 // indirect
	github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
	github.com/stretchr/testify v1.4.0 // indirect
	golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271 // indirect
)

Here is the code that I wrote which produces the mentioned errors: https://github.com/txtdirect/txtdirect/pull/298

Output of docker version:

Client:
 Version:           18.09.7-ce
 API version:       1.39
 Go version:        go1.12.6
 Git commit:        2d0083d657
 Built:             Tue Jul  2 01:00:04 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.09.7-ce
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.12.6
  Git commit:       2d0083d657
  Built:            Tue Jul  2 00:59:35 2019
  OS/Arch:          linux/amd64
  Experimental:     false

Output of docker info:

Containers: 152
 Running: 0
 Paused: 0
 Stopped: 152
Images: 175
Server Version: 18.09.7-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: f80d2850797c8132e995bbaa14ff665db5a1d3aa.m
runc version: 425e105d5a03fabd737a126ad93d62a9eeede87f
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.20.17-1-MANJARO
Operating System: Manjaro Linux
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.496GiB
Name: valhalla
ID: PCMA:ODGK:KMID:ORL4:SROV:M77F:X6FJ:P6CQ:RCD7:NRFV:DLOZ:YMKA
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Username: erbesharat
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

Additional environment details (AWS, VirtualBox, physical, etc.): The code is running on my local environment. Let me know if I’ve to provide any other information.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 11
  • Comments: 18 (6 by maintainers)

Commits related to this issue

Most upvoted comments

On the maintainers call today we discussed making client a dedicated module with its own tags (ie client/ will get its own go.mod and special tags client/) which, in theory, should work with go get.

@AkihiroSuda thx for the quick reply but now I am even more confused… I came from this page: https://godoc.org/github.com/docker/docker/client and github.com/docker/docker now redirects here. I want to programatically interact with docker from go, similar as I would when using dockerpy for python for example.

With your hint to use go get github.com/docker/docker@master I got the example from the doc page working.

Do I understand you correctly that I should use go get github.com/docker/engine@v19.03.5 if I want to work with a “released” version?

This is an existing issue. People who look for Docker’s Go client nearly 98% (I guess) of arrives to the GoDoc which says the following:

cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
	panic(err)
}

That’s alright. That make sense to me having go get github.com/docker/docker. But in that case we got the same issue what we had in the original comment.

Is it possible to somehow maintain these examples/versions/documentations in a consistent way?

I’m closing this, because the original question is answered.

Also, @cpuguy83 is working on mod-friendly client library in https://github.com/cpuguy83/go-docker

add replace in go.sum fix the bug

replace (
	github.com/docker/docker v1.13.1 => github.com/docker/engine v0.0.0-20191113042239-ea84732a7725
)

Use the latest version by putting this line in your go.mod

require (
	github.com/docker/docker master
)

go get github.com/docker/docker@master works for me, and it installed github.com/docker/docker v17.12.0-ce-rc1.0.20200713135139-d78b22cdf532+incompatible

go get github.com/docker/docker@master

@tnir, you may use any branch/tag. For instance:

  • require github.com/docker/docker 17.03.x after go get ends up as require github.com/docker/docker v1.13.2-0.20170712005057-ceb9e244d934
  • require github.com/docker/docker v17.03.2-ce after go get ends up as require github.com/docker/docker v1.13.2-0.20170601211448-f5ec1e2936dc
  • require github.com/docker/docker master after go get ends up as require github.com/docker/docker v1.4.2-0.20200109200802-5947fa1b3e44
  • require github.com/docker/docker v17.04.0-ce after go get ends up as require github.com/docker/docker v1.4.2-0.20170403171525-4845c567eb35

@Coac’s reply:

Use the latest version by putting this line in your go.mod

require (
	github.com/docker/docker master
)

is recommended by official docs:

When using the SDK, use the latest version you can, but at least the version that incorporates the API version with the features you need.

Pay attention that master will be resolved to certain commit (for example github.com/docker/docker v1.4.2-0.20200103225628-a9507c6f7662), when updating you will have to change it back to master (github.com/docker/docker master) so it will be properly resolved once again.

Try cli, err: = client.NewEnvClient() created. This is the old method https://github.com/moby/moby/commit/c8ff5ecc091278520a890ce58ed891ec354bf6d9