examples: add example for building buildkit itself

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
docker-18.09
Tonis Tiigi 2017-06-21 14:48:44 -07:00
parent 728de510f3
commit 944b1ae7a6
1 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,82 @@
package main
import (
"flag"
"os"
"github.com/tonistiigi/buildkit_poc/client/llb"
"github.com/tonistiigi/buildkit_poc/util/system"
)
func main() {
target := flag.String("target", "containerd", "target (standalone, containerd)")
flag.Parse()
bk := buildkit(*target == "containerd")
out := bk.Run(llb.Shlex("ls -l /bin")) // debug output
dt, err := out.Marshal()
if err != nil {
panic(err)
}
llb.WriteTo(dt, os.Stdout)
}
func goBuildBase() *llb.State {
goAlpine := llb.Image("docker.io/library/golang:1.8-alpine")
return goAlpine.
AddEnv("PATH", "/usr/local/go/bin:"+system.DefaultPathEnv).
AddEnv("GOPATH", "/go").
Run(llb.Shlex("apk add --no-cache g++ linux-headers")).
Run(llb.Shlex("apk add --no-cache git make")).Root()
}
func runc(version string) *llb.State {
return goBuildBase().
Run(llb.Shlex("git clone https://github.com/opencontainers/runc.git /go/src/github.com/opencontainers/runc")).
Dir("/go/src/github.com/opencontainers/runc").
Run(llb.Shlex("git checkout -q %s", version)).
Run(llb.Shlex("go build -o /usr/bin/runc ./")).Root()
}
func containerd(version string) *llb.State {
return goBuildBase().
Run(llb.Shlex("apk add --no-cache btrfs-progs-dev")).
Run(llb.Shlex("git clone https://github.com/containerd/containerd.git /go/src/github.com/containerd/containerd")).
Dir("/go/src/github.com/containerd/containerd").
Run(llb.Shlex("git checkout -q %s", version)).
Run(llb.Shlex("make bin/containerd")).Root()
}
func buildkit(withContainerd bool) *llb.State {
src := goBuildBase().
Run(llb.Shlex("git clone https://github.com/tonistiigi/buildkit_poc.git /go/src/github.com/tonistiigi/buildkit_poc")).
Dir("/go/src/github.com/tonistiigi/buildkit_poc")
builddStandalone := src.
Run(llb.Shlex("go build -o /bin/buildd-standalone -tags standalone ./cmd/buildd"))
builddContainerd := src.
Run(llb.Shlex("go build -o /bin/buildd-containerd -tags containerd ./cmd/buildd"))
buildctl := src.
Run(llb.Shlex("go build -o /bin/buildctl ./cmd/buildctl"))
r := llb.Image("docker.io/library/alpine:latest")
r = copy(buildctl.Root(), "/bin/buildctl", r, "/bin/")
r = copy(runc("v1.0.0-rc3"), "/usr/bin/runc", r, "/bin/")
if withContainerd {
r = copy(containerd("master"), "/go/src/github.com/containerd/containerd/bin/containerd", r, "/bin/")
r = copy(builddContainerd.Root(), "/bin/buildd-containerd", r, "/bin/")
} else {
r = copy(builddStandalone.Root(), "/bin/buildd-standalone", r, "/bin/")
}
return r
}
func copy(src *llb.State, srcPath string, dest *llb.State, destPath string) *llb.State {
cpImage := llb.Image("docker.io/library/alpine:latest")
cp := cpImage.Run(llb.Shlex("cp -a /src%s /dest%s", srcPath, destPath))
cp.AddMount("/src", src)
return cp.AddMount("/dest", dest)
}