Skip to content

Instantly share code, notes, and snippets.

@fsouza
Created April 3, 2017 20:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fsouza/b0bf3043827f8e39c4589e88cec067d8 to your computer and use it in GitHub Desktop.
Save fsouza/b0bf3043827f8e39c4589e88cec067d8 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"os"
"github.com/fsouza/go-dockerclient"
)
func main() {
var stdout bytes.Buffer
client, err := docker.NewClientFromEnv()
if err != nil {
panic(err)
}
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
container, err := client.CreateContainer(docker.CreateContainerOptions{
Config: &docker.Config{
Cmd: []string{"ls", "-la", "/tmp/pwd"},
Image: "ubuntu:14.04",
AttachStdout: true,
},
HostConfig: &docker.HostConfig{
Binds: []string{pwd + ":/tmp/pwd"},
},
})
if err != nil {
panic(err)
}
defer client.RemoveContainer(docker.RemoveContainerOptions{
Force: true,
ID: container.ID,
})
err = client.StartContainer(container.ID, nil)
if err != nil {
panic(err)
}
_, err = client.WaitContainer(container.ID)
if err != nil {
panic(err)
}
err = client.Logs(docker.LogsOptions{
Stdout: true,
Container: container.ID,
OutputStream: &stdout,
})
if err != nil {
panic(err)
}
fmt.Println(stdout.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment