-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (70 loc) · 2.18 KB
/
main.go
File metadata and controls
81 lines (70 loc) · 2.18 KB
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
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"fmt"
"os"
"dagger.io/dagger"
"golang.org/x/sync/errgroup"
)
func main() {
ctx := context.Background()
client, _ := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
defer client.Close()
// create cache volumes for docker certificates and docker images, containers and local volumes
certCache := client.CacheVolume("node")
dockerState := client.CacheVolume("docker-state")
// create the container with Dind with the docker daemon we will be using
docker, _ := client.Container().
From("docker:dind").
WithExposedPort(2376).
WithMountedCache("/var/lib/docker", dockerState, dagger.ContainerWithMountedCacheOpts{
Sharing: dagger.Private,
}).
WithMountedCache("/certs", certCache).
WithExec(nil, dagger.ContainerWithExecOpts{
InsecureRootCapabilities: true,
}).
AsService().
Start(ctx)
// Preparing the runner container
runner := client.Container().
From("docker:latest").
// The certificates generated in the DinD container is valid for
// the hostname "docker", so we will need to use that.
WithServiceBinding("docker", docker).
WithMountedCache("/certs", certCache).
WithEnvVariable("DOCKER_HOST", "tcp://docker:2376").
WithEnvVariable("DOCKER_TLS_CERTDIR", "/certs").
WithEnvVariable("DOCKER_CERT_PATH", "/certs/client").
WithEnvVariable("DOCKER_TLS_VERIFY", "1")
group := errgroup.Group{}
// Container example 1
// Run first container that pulls busybox.
group.Go(func() error {
_, _ = runner.
WithExec([]string{"docker", "pull", "busybox"}).
Sync(ctx)
return nil
})
// Container example 2
// Running second container that pulls alpine
group.Go(func() error {
_, _ = runner.
WithExec([]string{"docker", "pull", "alpine"}).
Sync(ctx)
return nil
})
// Wait for all goroutines to complete.
// The two first steps run in parallel.
if err := group.Wait(); err != nil {
fmt.Printf("errgroup tasks ended up with an error: %v\n", err)
} else {
fmt.Println("all works done successfully")
}
// Container example 3
// Running third container that shows alpine and busybox images
// fetched by the remote docker daemon container
_, _ = runner.
WithExec([]string{"docker", "images"}).
Sync(ctx)
}