Skip to content

Instantly share code, notes, and snippets.

@flavio
Last active November 24, 2021 17:05
Show Gist options
  • Save flavio/fdfd501775dad79b0e3b8d58dbe4a685 to your computer and use it in GitHub Desktop.
Save flavio/fdfd501775dad79b0e3b8d58dbe4a685 to your computer and use it in GitHub Desktop.
container-image-name
module github.com/flavio/container-image-name
go 1.16
require (
github.com/docker/distribution v2.7.1+incompatible
github.com/opencontainers/go-digest v1.0.0 // indirect
)
github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
package main
import (
_ "crypto/sha256"
_ "crypto/sha512"
"fmt"
"github.com/docker/distribution/reference"
"os"
)
type Image struct {
Registry string
Repository string
Tag string
Digest string
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Wrong number of args")
os.Exit(1)
}
refStr := os.Args[1]
named, err := reference.ParseNormalizedNamed(refStr)
if err != nil {
fmt.Printf("Cannot parse image name: %+v\n", err)
os.Exit(1)
}
// Add the latest lag if they did not provide one.
named = reference.TagNameOnly(named)
i := Image{
Registry: reference.Domain(named),
Repository: reference.Path(named),
}
// Add the tag if there was one.
if tagged, ok := named.(reference.Tagged); ok {
i.Tag = tagged.Tag()
} else {
i.Tag = "-"
}
// Add the digest if there was one.
if canonical, ok := named.(reference.Canonical); ok {
digest := canonical.Digest()
i.Digest = string(digest)
} else {
i.Digest = "-"
}
fmt.Printf("%s - %+v", named, i)
}
#!/bin/bash
INPUT_NAMES=(
"busybox"
"test.com:tag"
"test.com:5000"
"test.com/repo:tag"
"test:5000/repo:tag"
"test:5000/repo@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
"test:5000/repo:tag@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
"lowercase:Uppercase"
"sub-dom1.foo.com/bar/baz/quux"
"sub-dom1.foo.com/bar/baz/quux:some-long-tag"
"b.gcr.io/test.example.com/my-app:test.example.com"
"xn--n3h.com/myimage:xn--n3h.com"
"xn--7o8h.com/myimage:xn--7o8h.com@sha512:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
"foo_bar.com:8080"
"foo/foo_bar.com:8080"
)
for image in "${INPUT_NAMES[@]}"
do
echo "$image -> $(./container-image-name ${image})"
done
@flavio
Copy link
Author

flavio commented Nov 24, 2021

A simple way to see how container image names are actually parsed by Docker.

Building

  • Create a directory called container-image-name
  • go build

Usage

Just run:

./test.sh

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