daily pastebin goal
42%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func BuildImage(dockerBuildCtxDir, tagName string) error {
  2.     ctx, cancel := context.WithTimeout(context.Background(), time.Duration(300)*time.Second)
  3.     defer cancel()
  4.     dockerFileTarReader, err := tarDirectory(dockerBuildCtxDir)
  5.         //this above method will create a tar file from the directory passed in. The return has to be of type io.Reader
  6.     if err != nil {
  7.         log.Printf("Error in taring the docker root folder - %s", err.Error())
  8.         return err
  9.     }
  10.     buildArgs := make(map[string]*string)
  11.     // add any build args if you want to
  12.         buildArgs["ENV"] = os.Getenv("GO_ENV")
  13.  
  14.     resp, err := cli.ImageBuild(
  15.         ctx,
  16.         dockerFileTarReader,
  17.         types.ImageBuildOptions{
  18.             Dockerfile: "Dockerfile",
  19.             Tags:       []string{tagName},
  20.             NoCache:    true,
  21.             Remove:     true,
  22.             BuildArgs:  buildArgs,
  23.         }) //cli is the docker client instance created from the engine-api
  24.     if err != nil {
  25.         log.Println(err, " :unable to build docker image")
  26.         return err
  27.     }
  28.     return writeToLog(resp.Body)
  29. }
  30.  
  31. //writes from the build response to the log
  32. func writeToLog(reader io.ReadCloser) error {
  33.     defer reader.Close()
  34.     rd := bufio.NewReader(reader)
  35.     for {
  36.         n, _, err := rd.ReadLine()
  37.         if err != nil && err == io.EOF {
  38.             break
  39.         } else if err != nil {
  40.             return err
  41.         }
  42.         log.Println(string(n))
  43.     }
  44.     return nil
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top