diff --git a/new.go b/new.go index c7e330c1302..a74d4223a44 100644 --- a/new.go +++ b/new.go @@ -15,6 +15,7 @@ import ( "github.com/containers/image/v5/transports" "github.com/containers/image/v5/types" "github.com/containers/storage" + "github.com/containers/storage/pkg/stringid" digest "github.com/opencontainers/go-digest" v1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/openshift/imagebuilder" @@ -48,6 +49,15 @@ func getImageName(name string, img *storage.Image) string { func imageNamePrefix(imageName string) string { prefix := imageName + if d, err := digest.Parse(imageName); err == nil { + prefix = d.Encoded() + if len(prefix) > 12 { + prefix = prefix[:12] + } + } + if stringid.ValidateID(prefix) == nil { + prefix = stringid.TruncateID(prefix) + } s := strings.Split(prefix, ":") if len(s) > 0 { prefix = s[0] diff --git a/tests/from.bats b/tests/from.bats index 60e507ed285..133fd9cbbc3 100644 --- a/tests/from.bats +++ b/tests/from.bats @@ -648,3 +648,19 @@ load helpers expect_output "$tmp" BOGUS_PROXY=$tmp run_buildah 1 run $cid printenv BOGUS_PROXY } + +@test "from-image-by-id" { + _prefetch busybox + run_buildah from --cidfile ${TESTDIR}/cid busybox + cid=$(cat ${TESTDIR}/cid) + createrandom ${TESTDIR}/randomfile + run_buildah copy ${cid} ${TESTDIR}/randomfile / + run_buildah commit --iidfile ${TESTDIR}/iid ${cid} + iid=$(cat ${TESTDIR}/iid) + run_buildah from --cidfile ${TESTDIR}/cid2 ${iid} + cid2=$(cat ${TESTDIR}/cid2) + run_buildah run ${cid2} hostname -f + truncated=${iid##*:} + truncated=$(echo ${truncated} | cut -c-12) + expect_output ${truncated}-working-container +}