Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// buildLocalImage build a docker image from the supplied `path` parameter.
// The image built is intended to be pushed to a local docker registry.
// This function assumes there is a Dockerfile in the dir
func buildLocalImage(path string) error {
// get current working dir, to resolve the path to Dockerfile
wd, err := os.Getwd()
if err != nil {
return err
}
// create a docker buildContext by `archiving` the files
// the target dir
buildCtx, err := createBuildContext(path)
if err != nil {
return err
}
// form a unique docker tag. the first string seg is the local docker registry host
tag := fmt.Sprintf("%s%s%s", "docker-registry:5000/", build.Name(), p.md5()[:6])
ctx := context.Background()
// build image. reader can be used to get output from docker deamon
reader, err := p.client.ImageBuild(ctx, buildCtx, types.ImageBuildOptions{
Dockerfile: "Dockerfile", PullParent: true, Tags: []string{tag}, Remove: true, NoCache: true,
})
if err != nil {
return err
}
for {
buf := make([]byte, 512)
_, err := reader.Body.Read(buf)
if err != nil {
if err == io.EOF {
break
}
log.Println("error reading response ", err)
continue
}
// print outputs
log.Println(string(buf[:]))
}
// yay! no errors
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment