Docker Community Forums

Share and learn in the Docker community.

Golang API "ImagePull" error [Solved]

I’ve been attempting to use the docker golang API today, and have struck a problem when attempting to pull images. The rest of the project that utilizes the API works fine (starting, stopping containers, etc…), if the image is already pulled manually from the console.

For reference, I was following the guide at https://docs.docker.com/engine/api/get-started/#run-a-container

ctx := context.Background()

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

_, err = cli.ImagePull(ctx, "docker.io/library/hello-world", types.ImagePullOptions{})
if err != nil {
  panic(err)
}

Results in:

panic: Error: No such image: hello-world
# docker version
Client:
 Version:      17.06.0-ce
 API version:  1.30
 Go version:   go1.8.3
 Git commit:   02c1d87
 Built:        Fri Jun 23 21:20:36 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.06.0-ce
 API version:  1.30 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   02c1d87
 Built:        Fri Jun 23 21:21:56 2017
 OS/Arch:      linux/amd64
 Experimental: false

Ah, found the issue.
ImagePull returns a reader that needs to be handled.
For example

if r, err := cli.ImagePull(ctx, "hello-world", types.ImagePullOptions{}); err != nil {
        panic(err)
} else {
        io.Copy(os.Stdout, r)
}
1 Like

why do we need to handle reader?
what does it do in respect to API calls?