forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
45 lines (39 loc) · 1.08 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package container
import (
"context"
"fmt"
"io"
"github.com/docker/docker/api/types"
dcontainer "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/pkg/errors"
)
func Run(ctx context.Context, docker client.CommonAPIClient, ctrID string, out, errOut io.Writer) error {
bodyChan, errChan := docker.ContainerWait(ctx, ctrID, dcontainer.WaitConditionNextExit)
if err := docker.ContainerStart(ctx, ctrID, types.ContainerStartOptions{}); err != nil {
return errors.Wrap(err, "container start")
}
logs, err := docker.ContainerLogs(ctx, ctrID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
if err != nil {
return errors.Wrap(err, "container logs stdout")
}
copyErr := make(chan error)
go func() {
_, err := stdcopy.StdCopy(out, errOut, logs)
copyErr <- err
}()
select {
case body := <-bodyChan:
if body.StatusCode != 0 {
return fmt.Errorf("failed with status code: %d", body.StatusCode)
}
case err := <-errChan:
return err
}
return <-copyErr
}