-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_docker_image.go
91 lines (82 loc) · 2.01 KB
/
create_docker_image.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package docker
import (
"context"
"fmt"
"os"
"os/exec"
"github.com/Doer-org/ketos/internal/docker"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
)
func CreateImage(dockerFile bool, languageType string, directory string, dockerfilename string) error {
if dockerFile {
err := createImageWithDockerFile(directory, dockerfilename)
if err != nil {
return err
}
} else {
err := createImageWithBuildPacks(directory, languageType)
if err != nil {
return err
}
}
return nil
}
func createImageWithDockerFile(directory string, dockerfilename string) error {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
tar, err := archive.TarWithOptions(directory, &archive.TarOptions{})
if err != nil {
return err
}
fmt.Println("Creating image with Dockerfile")
fmt.Println("Path: ", directory)
fmt.Println("Dockerfile: ", dockerfilename)
defer tar.Close()
imageBuildResponse, err := cli.ImageBuild(
ctx,
tar,
types.ImageBuildOptions{
Tags: []string{docker.ImageName},
Dockerfile: dockerfilename,
Remove: true,
NoCache: true,
},
)
if err != nil {
return err
}
defer imageBuildResponse.Body.Close()
return nil
}
func createImageWithBuildPacks(directory string, language string) error {
builder := responseBuilder(language)
cmd := exec.Command("pack", "build", docker.ImageName, "--builder", builder, "--path", directory)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func responseBuilder(language string) string {
switch language {
case "node":
return "gcr.io/buildpacks/builder:v1"
case "go":
return "gcr.io/buildpacks/builder:v1"
case "python":
return "gcr.io/buildpacks/builder:v1"
case "java":
return "gcr.io/buildpacks/builder:v1"
case "ruby":
return "paketobuildpacks/builder:base"
default:
return "gcr.io/buildpacks/builder:v1"
}
}