Skip to content

Instantly share code, notes, and snippets.

@weakpixel
Last active November 1, 2023 06:54
Show Gist options
  • Save weakpixel/b9d3ddf25e52e88ed5608a1f30bd87db to your computer and use it in GitHub Desktop.
Save weakpixel/b9d3ddf25e52e88ed5608a1f30bd87db to your computer and use it in GitHub Desktop.
Use Crane to push a Docker multi image to registry
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)
}
}
}
}
@jinteng123
Copy link

how to push http

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment