/
docker.go
42 lines (35 loc) · 856 Bytes
/
docker.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
package docker
import (
"context"
"fmt"
"io"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
var (
isLocalImage = os.Getenv("LOCAL_IMAGE")
imageRef = os.Getenv("IMAGE_NAME")
imageUser = os.Getenv("IMAGE_USER")
imageCmd = os.Getenv("IMAGE_CMD")
cli *client.Client
)
func Setup() error {
var err error
cli, err = client.NewClientWithOpts()
if err != nil {
return fmt.Errorf("failed to create docker client: %w", err)
}
ctx := context.Background()
if len(isLocalImage) == 0 || isLocalImage == "false" {
reader, err := cli.ImagePull(ctx, imageRef, types.ImagePullOptions{})
if err != nil {
return fmt.Errorf("failed to pull image: %w", err)
}
_, err = io.Copy(os.Stdout, reader)
if err != nil {
return fmt.Errorf("failed to copy stdout: %w", err)
}
}
return nil
}