`docker run` using Golang API (Docker docs)

I am trying to use Docker’s tutorial in recreating a docker run. Here is the following code from online tutorial

 package main

 import (


 func main() {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {

_, err = cli.ImagePull(ctx, "alpine", types.ImagePullOptions{})
if err != nil {

resp, err := cli.ContainerCreate(ctx, &container.Config{
    Image: "alpine",
    Cmd:   []string{"echo", "hello world"},
}, nil, nil, "")
if err != nil {

if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{});     err != nil {

if _, err = cli.ContainerWait(ctx, resp.ID); err != nil {

out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {

io.Copy(os.Stdout, out)

The problem I see with this is, if ‘alpine’ docker, is not available locally, it doesn’t pull the latest and ends up throwing an error.
XXXXX$ go run go_docker.go
panic: Error: No such image: alpine

  • Docker with WebHDFS
  • How to test code that works with Docker?
  • Retrieve docker image ID
  • Boot2Docker (on Windows) running Mongo with shared folder (This file system is not supported)
  • Running VNCserver on docker container
  • Why not run uwsgi instances as root
  •     goroutine 1 [running]:
          panic(0x27ffa0, 0xc4202afa50)
        /usr/local/go/src/runtime/panic.go:500 +0x1a1
        /Users/rvenkatesh/go_coding/raghu_test_code/go_docker.go:30 +0x592
        exit status 2

    But when I run the commandline equivalent, I see

        XXXX$ docker run alpine echo hello world
        Unable to find image 'alpine:latest' locally
        latest: Pulling from library/alpine 
        627beaf3eaaf: Pull complete
        Status: Downloaded newer image for alpine:latest
        hello world

    I tried looking through Go client, do I need to tweak anything with ImagePull function? Any help here would be appreciated!

    Here is the link to the docs https://docs.docker.com/engine/api/getting-started/

    Update: I had tested the same tutorial for python version, and it worked just fine. I wonder if the Golang page needs update.

  • Kuberenets Web UI (Dashboard) missing graphs
  • Forwarding traffic between two interfaces
  • Docker: updating image and registry
  • How to Copy Files From Docker Ubuntu Container to Windows Host
  • ASP.NET Core EF commands: Unable to connect to any of the specified MySQL hosts
  • Authenticating Jupyter Notebook inside Docker keeps resulting in invalid token
  • 3 Solutions collect form web for “`docker run` using Golang API (Docker docs)”

    The docker client is open source and written in Go, so you can see exactly how they’ve implemented their version. I believe the relevant code is in the container/create.go pullImage function.

    Image.Pull returns an io.Reader which you must read and close; if you do not the connection will be closed before the image is pulled.

    You can just discard the contents of it and close it, then the pull will work.

    Was having the same issue, the “Pull” didn’t seem to be working. Found a Fix though.

    1) Modify your pull line to

    pullstat, err = cli.ImagePull(ctx, "alpine", types.ImagePullOptions{})

    and add


    after the ImagePull

    I haven’t tried doing an


    but that’s on my list of things to try next.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.