Last active
February 23, 2017 18:35
-
-
Save repejota/bfd7567c6090651a54f2444cd1438880 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"context" | |
"fmt" | |
"io" | |
"os" | |
"github.com/docker/docker/api/types" | |
"github.com/docker/docker/api/types/container" | |
"github.com/docker/docker/client" | |
) | |
func main() { | |
cli, err := client.NewEnvClient() | |
if err != nil { | |
panic(err) | |
} | |
defer cli.Close() | |
ctx := context.Background() | |
_, err = cli.ImagePull(ctx, "alpine", types.ImagePullOptions{}) | |
if err != nil { | |
panic(err) | |
} | |
containerConfig := &container.Config{ | |
Image: "alpine", | |
Cmd: []string{"echo", "hello world"}, | |
} | |
resp, err := cli.ContainerCreate(ctx, containerConfig, nil, nil, "") | |
if err != nil { | |
panic(err) | |
} | |
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { | |
panic(err) | |
} | |
statusCode, err := cli.ContainerWait(ctx, resp.ID) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(statusCode) | |
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true}) | |
if err != nil { | |
panic(err) | |
} | |
io.Copy(os.Stdout, out) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment