Created
April 3, 2017 20:04
-
-
Save fsouza/b0bf3043827f8e39c4589e88cec067d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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