Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@developer-guy
Created March 26, 2021 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developer-guy/e9bee872f145d2716b702152175c4f12 to your computer and use it in GitHub Desktop.
Save developer-guy/e9bee872f145d2716b702152175c4f12 to your computer and use it in GitHub Desktop.
Get OCI Image Config programmatically using go-containerregistry as Go module
package main
import (
"encoding/json"
"log"
"os"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
func main() {
basicAuthn := &authn.Basic{
Username: os.Getenv("DOCKER_USERNAME"),
Password: os.Getenv("DOCKER_PASSWORD"),
}
withAuthOption := remote.WithAuth(basicAuthn)
options := []remote.Option{withAuthOption}
imageName := os.Args[1]
ref, err := name.ParseReference(imageName)
if err != nil {
log.Fatalf("cannot parse reference of the image %s , detail: %v", imageName, err)
}
descriptor, err := remote.Get(ref, options...)
if err != nil {
log.Fatalf("cannot get image %s , detail: %v", imageName, err)
}
image, err := descriptor.Image()
if err != nil {
log.Fatalf("cannot convert image %s descriptor to v1.Image, detail: %v", imageName, err)
}
configFile, err := image.ConfigFile()
if err != nil {
log.Fatalf("cannot extract config file of image %s, detail: %v", imageName, err)
}
prettyJSON, err := json.MarshalIndent(configFile, "", " ")
log.Println(string(prettyJSON))
}
@developer-guy
Copy link
Author

$ DOCKER_USERNAME=<username> DOCKER_PASSWORD=<password> go run -v ./main.go <image> | jq .
Screen Shot 2021-03-29 at 10 14 46

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