package main import ( "io" "os" "github.com/docker/docker/client" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "golang.org/x/net/context" "github.com/docker/go-connections/nat" ) func main() { ctx := context.Background() cli, err := client.NewEnvClient() if err != nil { panic(err) } _, err = cli.ImagePull(ctx, "ghost:latest", types.ImagePullOptions{}) if err != nil { panic(err) } config := &container.Config{ Image: "ghost", ExposedPorts: nat.PortSet{ "8080/tcp": struct{}{}, }, } hostConfig := &container.HostConfig{ PortBindings: nat.PortMap{ "2368/tcp": []nat.PortBinding{ { HostPort: "8080", }, }, }, } resp, err := cli.ContainerCreate(ctx, config, hostConfig, nil, "") if err != nil { panic(err) } if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { panic(err) } out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true, Details: true, Follow: true, ShowStderr: true, Timestamps: true}) if err != nil { panic(err) } io.Copy(os.Stdout, out) statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning) select { case err := <-errCh: if err != nil { panic(err) } case <-statusCh: } }