Use Crane to push a Docker multi image to registry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io" | |
"os" | |
"path" | |
"github.com/google/go-containerregistry/pkg/crane" | |
"github.com/google/go-containerregistry/pkg/name" | |
"github.com/google/go-containerregistry/pkg/v1/tarball" | |
) | |
func pathOpener(path string) tarball.Opener { | |
return func() (io.ReadCloser, error) { | |
return os.Open(path) | |
} | |
} | |
// docker save alpine busybox > ./images.tar | |
// <app> ./images.tar http://localhost:5000 | |
func main() { | |
manifest := flag.Bool("m", false, "Set to show manifest only") | |
flag.Parse() | |
args := flag.Args() | |
file := args[0] | |
repo := args[1] | |
fmt.Println("Tarball: ", file) | |
fmt.Println("Target Repo: ", repo) | |
man, err := tarball.LoadManifest(pathOpener(file)) | |
if err != nil { | |
panic(err) | |
} | |
if *manifest { | |
for _, d := range man { | |
for _, t := range d.RepoTags { | |
fmt.Println(t) | |
} | |
} | |
return | |
} | |
for _, d := range man { | |
for _, t := range d.RepoTags { | |
tag, err := name.NewTag(t) | |
if err != nil { | |
panic(err) | |
} | |
i, err := tarball.Image(pathOpener(file), &tag) | |
if err != nil { | |
panic(err) | |
} | |
target := path.Join(repo, t) | |
fmt.Printf("Push %s -> %s", t, target) | |
fmt.Println() | |
err = crane.Push(i, target) | |
if err != nil { | |
panic(err) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment