-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets.go
71 lines (54 loc) · 1.56 KB
/
snippets.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Log into the registry
var authConfig = registry.AuthConfig{
Username: "gsaenger",
Password: os.Getenv("DOCKER_PASS"),
ServerAddress: "https://index.docker.io/v1/",
}
authConfigBytes, _ := json.Marshal(authConfig)
authConfigEncoded := base64.URLEncoding.EncodeToString(authConfigBytes)
// Push image
tag := "gsaenger/hello-go"
pushOpts := types.ImagePushOptions{RegistryAuth: authConfigEncoded}
pushLogs, err := dockerClient.ImagePush(context.Background(), tag, pushOpts)
if err != nil {
fmt.Println(err.Error())
return
}
defer pushLogs.Close()
err = printOutput(pushLogs)
if err != nil {
fmt.Println(err.Error())
return
}
// Start a session for BuildKit
cfg, err := getDefaultDockerConfig()
ctx := context.Background()
sess, err := session.NewSession(ctx, "pulumi-docker", identity.NewID())
if err != nil {
fmt.Println(err.Error())
return
}
dockerAuthProvider := authprovider.NewDockerAuthProvider(cfg, nil)
sess.Allow(dockerAuthProvider)
dialSession := func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) {
return dockerClient.DialHijack(ctx, "/session", proto, meta)
}
go func() {
err := sess.Run(ctx, dialSession)
if err != nil {
fmt.Println(err.Error())
return
}
}()
defer sess.Close()
opts.SessionID = sess.ID()
dockerClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
fmt.Println(err.Error())
return
}
// Run the container
startOpts := container.StartOptions{}
if err := dockerClient.ContainerStart(context.Background(), "4f2eb408263a", startOpts); err != nil {
panic(err)
}