Created
February 15, 2021 19:57
-
-
Save developer-guy/2664e217d080f9a3ba34a52e3ed67425 to your computer and use it in GitHub Desktop.
Copying images without pulling it using containers/image module in go without need docker
This file contains hidden or 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 ( | |
| "context" | |
| "fmt" | |
| "github.com/containers/image/copy" | |
| "github.com/containers/image/docker" | |
| "github.com/containers/image/manifest" | |
| "github.com/containers/image/signature" | |
| "github.com/containers/image/types" | |
| "os" | |
| ) | |
| func main() { | |
| todo := context.TODO() | |
| sourceImageRef, err := docker.ParseReference("//hello-world") | |
| if err != nil { | |
| panic(err) | |
| } | |
| dockerID := os.GetEnv("DOCKER_ID") | |
| destImageRef, err := docker.ParseReference("//" + dockerID + "/hello-world") | |
| if err != nil { | |
| panic(err) | |
| } | |
| reqs := []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()} | |
| pc, err := signature.NewPolicyContext(&signature.Policy{Default: reqs}) | |
| if err != nil { | |
| panic(err) | |
| } | |
| m, err := copy.Image(todo, pc, destImageRef, sourceImageRef, | |
| ©.Options{ | |
| SourceCtx: &types.SystemContext{OSChoice: "linux", ArchitectureChoice: "amd64"}, | |
| DestinationCtx: &types.SystemContext{OSChoice: "linux", ArchitectureChoice: "amd64", | |
| DockerAuthConfig: &types.DockerAuthConfig{Username: os.Getenv("DOCKER_USERNAME"), Password: os.Getenv("DOCKER_PASSWORD")}}}, | |
| ) | |
| digest, err := manifest.Digest(m) | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Printf("Copied digest: %s", digest.String()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment