Executing docker command using golang exec fails

I am using cmd.go (see below) to execute a docker command but it fails. I do the following steps to execute and get the following error.

go build
sudo ./cmd


  • Docker 1.9 overlay network - access from host
  • Exposing a WSGI app from a Docker container
  • Unable to mount MySQL data volume to Kubernetes Minikube pod
  • Should servlet running under tomcat in a container exit when it is not able to reach DB container?
  • run rscript on local in a docker image
  • Docker Error response from daemon: service endpoint with name.
  • docker run -v ~/exp/a.out:/a.out ubuntu:14.04 /a.out -m 10m
    2014/10/16 14:32:12 exit status 1

    On the other hand running directly as

    sudo docker run -v ~/exp/a.out:/a.out ubuntu:14.04 /a.out -m 10m

    results in the correct output of a.out.

    Hello World

    This is the code of cmd.go. How can I get it to work? Thanks!

    package main
    import (
    func ExampleCmd_Output() {
            //out, err := exec.Command("date", "--version").Output()   // This works
            //out, err := exec.Command("docker", "--version").Output() // This works
            //out, err := exec.Command(cmd, "images").Output() // Even docker images command works!
            cmd := "docker"
            cmdArgs := []string{"run", "-v", "~/exp/a.out:/a.out", "ubuntu:14.04", "/a.out", "-m", "10m"}
            fmt.Println(cmd + " " + strings.Join(cmdArgs, " "))
            out, err := exec.Command(cmd, cmdArgs...).Output()
            if err != nil {
            fmt.Printf("%s", out)
    func main() {

    EDIT: After a comment, I tried executing the command “docker images”. It works if I run the executable with sudo. That is, I am using the following line in the code now.

    out, err := exec.Command(cmd, "images").Output()

    After doing go build and running “sudo ./cmd”, I get the output of docker images command. However, without sudo, I still get exit status 1. But with docker run command above even with sudo, I don’t get an output.

  • Jenkins running in Docker on AWS EC2 can't retrieve http URLs, only https
  • Do Docker images save the memory state?
  • Docker Cloud autotest cant find service
  • What is the tar-data.json.gz in /var/lib/docker/graph/<image GUID>
  • Can't start docker with postgres
  • Node.js tcp sockets + linked Docker containers
  • One Solution collect form web for “Executing docker command using golang exec fails”

    Thanks to Os Exec Sudo Command in Go, I am now able to do what I want.

    func main() {
      cmdStr := "sudo docker run -v ~/exp/a.out:/a.out ubuntu:14.04 /a.out -m 10m"
      out, _ := exec.Command("/bin/sh", "-c", cmdStr).Output()  
      fmt.Printf("%s", out)


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