diff --git a/new.go b/new.go index 7a306da225..5780224549 100644 --- a/new.go +++ b/new.go @@ -77,6 +77,24 @@ func pullAndFindImage(store storage.Store, imageName string, options BuilderOpti return img, ref, nil } +func getImageName(name string, img *storage.Image) string { + imageName := name + if len(img.Names) > 0 { + imageName = img.Names[0] + // When the image used by the container is a tagged image + // the container name might be set to the original image instead of + // the image given in the "form" command line. + // This loop is supposed to fix this. + for _, n := range img.Names { + if strings.Contains(n, name) { + imageName = n + break + } + } + } + return imageName +} + func imageNamePrefix(imageName string) string { prefix := imageName s := strings.Split(imageName, "/") @@ -207,9 +225,7 @@ func newBuilder(store storage.Store, options BuilderOptions) (*Builder, error) { image := options.FromImage imageID := "" if img != nil { - if len(img.Names) > 0 { - image = img.Names[0] - } + image = getImageName(image, img) imageID = img.ID } if manifest, config, err = imageManifestAndConfig(ref, systemContext); err != nil { diff --git a/new_test.go b/new_test.go new file mode 100644 index 0000000000..60fb31c682 --- /dev/null +++ b/new_test.go @@ -0,0 +1,28 @@ +package buildah + +import ( + "testing" + + "github.com/containers/storage" +) + +func TestGetImageName(t *testing.T) { + tt := []struct { + caseName string + name string + names []string + expected string + }{ + {"tagged image", "busybox1", []string{"docker.io/library/busybox:latest", "docker.io/library/busybox1:latest"}, "docker.io/library/busybox1:latest"}, + {"image name not in the resolved image names", "image1", []string{"docker.io/library/busybox:latest", "docker.io/library/busybox1:latest"}, "docker.io/library/busybox:latest"}, + {"resolved image with empty name list", "image1", []string{}, "image1"}, + } + + for _, tc := range tt { + img := &storage.Image{Names: tc.names} + res := getImageName(tc.name, img) + if res != tc.expected { + t.Errorf("test case '%s' failed: expected %#v but got %#v", tc.caseName, tc.expected, res) + } + } +} diff --git a/tests/from.bats b/tests/from.bats index 1447ba8440..095257fb6a 100644 --- a/tests/from.bats +++ b/tests/from.bats @@ -73,7 +73,6 @@ load helpers } @test "from-authenticate-cert-and-creds" { - mkdir -p ${TESTDIR}/auth # Create creds and store in ${TESTDIR}/auth/htpasswd # docker run --entrypoint htpasswd registry:2 -Bbn testuser testpassword > ${TESTDIR}/auth/htpasswd @@ -112,3 +111,14 @@ load helpers # buildah rm $ctrid # buildah rmi -f $(buildah --debug=false images -q) } + +@test "from-tagged-image" { + # Github #396: Make sure the container name starts with the correct image even when it's tagged. + cid=$(buildah from --pull=false --signature-policy ${TESTSDIR}/policy.json scratch) + buildah commit --signature-policy ${TESTSDIR}/policy.json "$cid" scratch2 + buildah rm $cid + buildah tag scratch2 scratch3 + cid=$(buildah from --signature-policy ${TESTSDIR}/policy.json scratch3) + [ "$cid" == scratch3-working-container ] + buildah rmi -f --all +}