Skip to content

Commit

Permalink
add feature to export to docker tarball (#140)
Browse files Browse the repository at this point in the history
* add feature to export to docker tarball

* throw error if multiple platforms specified for tar export
  • Loading branch information
agouin committed Jun 23, 2023
1 parent 9a6413b commit 52b76cd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func (h *HeighlinerBuilder) buildChainNodeDockerImage(
buildKitOptions.Platform = buildCfg.Platform
}
buildKitOptions.NoCache = buildCfg.NoCache
if err := docker.BuildDockerImageWithBuildKit(ctx, reldir, imageTags, push, buildArgs, buildKitOptions); err != nil {
if err := docker.BuildDockerImageWithBuildKit(ctx, reldir, imageTags, push, buildCfg.TarExportPath, buildArgs, buildKitOptions); err != nil {
return err
}
} else {
Expand Down
1 change: 1 addition & 0 deletions builder/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type ChainNodeDockerBuildConfig struct {
type HeighlinerDockerBuildConfig struct {
ContainerRegistry string
SkipPush bool
TarExportPath string
UseBuildKit bool
BuildKitAddr string
Platform string
Expand Down
2 changes: 2 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
flagNumber = "number"
flagParallel = "parallel"
flagSkip = "skip"
flagTarExport = "tar-export-path"
flagLatest = "latest"
flagLocal = "local"
flagUseBuildkit = "use-buildkit"
Expand Down Expand Up @@ -158,6 +159,7 @@ An optional flag --tag/-t is now available to override the resulting docker imag
// Docker specific flags
buildCmd.PersistentFlags().StringVarP(&buildConfig.ContainerRegistry, flagRegistry, "r", "", "Docker Container Registry for pushing images")
buildCmd.PersistentFlags().BoolVarP(&buildConfig.SkipPush, flagSkip, "s", false, "Skip pushing images to registry")
buildCmd.PersistentFlags().StringVar(&buildConfig.TarExportPath, flagTarExport, "", "File path to export built image as docker tarball")
buildCmd.PersistentFlags().BoolVarP(&buildConfig.UseBuildKit, flagUseBuildkit, "b", false, "Use buildkit to build multi-arch images")
buildCmd.PersistentFlags().StringVar(&buildConfig.BuildKitAddr, flagBuildkitAddr, docker.BuildKitSock, "Address of the buildkit socket, can be unix, tcp, ssl")
buildCmd.PersistentFlags().StringVarP(&buildConfig.Platform, flagPlatform, "p", docker.DefaultPlatforms, "Platforms to build (only applies to buildkit builds with -b)")
Expand Down
53 changes: 44 additions & 9 deletions docker/buildkit.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package docker

import (
"bufio"
"context"
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -37,11 +39,24 @@ func GetDefaultBuildKitOptions() BuildKitOptions {
}
}

type WriteCloser struct {
f *os.File
*bufio.Writer
}

func (wc *WriteCloser) Close() error {
if err := wc.Flush(); err != nil {
return err
}
return wc.f.Close()
}

func BuildDockerImageWithBuildKit(
ctx context.Context,
dockerfileDir string,
tags []string,
push bool,
tarExport string,
args map[string]string,
buildKitOptions BuildKitOptions,
) error {
Expand All @@ -54,16 +69,36 @@ func BuildDockerImageWithBuildKit(

eg, ctx := errgroup.WithContext(ctx)

export := client.ExportEntry{
Type: "image",
Attrs: map[string]string{
"name": strings.Join(tags, ","),
},
}
if push {
export.Attrs["push"] = "true"
exports := make([]client.ExportEntry, 1)

if tarExport != "" {
if len(strings.Split(buildKitOptions.Platform, ",")) > 1 {
return fmt.Errorf("when using tar-export-path, only one platform is supported")
}

exports[0] = client.ExportEntry{
Type: "docker",
Output: func(m map[string]string) (io.WriteCloser, error) {
f, err := os.Create(tarExport)
if err != nil {
return nil, err
}

return &WriteCloser{f, bufio.NewWriter(f)}, nil
},
}
} else {
export := client.ExportEntry{
Type: "image",
Attrs: map[string]string{
"name": strings.Join(tags, ","),
},
}
if push {
export.Attrs["push"] = "true"
}
exports[0] = export
}
exports := []client.ExportEntry{export}

opts := map[string]string{
"platform": buildKitOptions.Platform,
Expand Down

0 comments on commit 52b76cd

Please sign in to comment.