Skip to content

Instantly share code, notes, and snippets.

@fsouza
Created November 27, 2017 19:45
Show Gist options
  • Save fsouza/3ca8aa82c9e027eee5b6e3d939e7db6b to your computer and use it in GitHub Desktop.
Save fsouza/3ca8aa82c9e027eee5b6e3d939e7db6b to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/fsouza/go-dockerclient"
"github.com/kubernetes/kompose/pkg/utils/archive"
)
func main() {
endpoint := "unix:///var/run/docker.sock"
client, err := docker.NewClient(endpoint)
if err != nil {
fmt.Println(err)
}
source := "/home/snarwade/go/src/github.com/kedgeproject/json-schema-generator"
image := "docker.io/surajnarwade/kedgeschema"
// Create a temporary file for tarball image packaging
tmpFile, err := ioutil.TempFile("/tmp", "kompose-image-build-")
if err != nil {
fmt.Println(err)
}
// Create a tarball of the source directory in order to build the resulting image
err = archive.CreateTarball(strings.Join([]string{source, ""}, "/"), tmpFile.Name())
if err != nil {
fmt.Println(err, "Unable to create a tarball")
}
// Load the file into memory
tarballSource, err := os.Open(tmpFile.Name())
if err != nil {
fmt.Println(err, "Unable to load tarball into memory")
}
// Let's create all the options for the image building.
outputBuffer := bytes.NewBuffer(nil)
opts := docker.BuildImageOptions{
Dockerfile: "scripts/Dockerfile",
Name: image,
InputStream: tarballSource,
OutputStream: outputBuffer,
}
// Build it!
if err := client.BuildImage(opts); err != nil {
fmt.Println(err, "Unable to build image")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment