Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I re-tag an image in ECR?

I'm trying to follow along with Retagging an Image with the AWS CLI and am beginning to suspect this guide is out-of-date. I have a number of Docker images pushed into an ECR repository - let's call it "myappserver." And so I can run commands like this one to see a list of all the images in the ECR repository:

aws ecr describe-images --repository-name myappserver

The output I get from that command looks something like this:

{
    "imageDetails": [
        {
            "registryId": "123456789012",
            "repositoryName": "myappserver",
            "imageDigest": "sha256:1234...",
            "imageSizeInBytes": 33805114,
            "imagePushedAt": 1525881170.0
        },
        {
            "registryId": "123456789012",
            "repositoryName": "myappserver",
            "imageDigest": "sha256:1234...",
            "imageTags": [
                "latest"
            ],
            "imageSizeInBytes": 333805137,
            "imagePushedAt": 1525892193.0
        },
        ...
    ]
}

Because not all of my images have tags any more, I want to identify them by imageDigest (instead of imageTag like the guide does), which should be fine. However, the command that the guide offers doesn't seem to work any more. It says:

Use the batch-get-image command to get the image manifest for the image to retag and write it to an environment variable. In this example, the manifest for an image with the tag, latest, in the repository, amazonlinux, is written to the environment variable, MANIFEST.

MANIFEST=$(aws ecr batch-get-image --repository-name amazonlinux --image-ids imageTag=latest --query images[].imageManifest --output text)

So, naturally, I try to run this command:

aws ecr batch-get-image --repository-name myappserver --image-ids imageDigest=sha256:1234... --query images[].imageManifest --output text

But the response I get in the terminal is:

zsh: no matches found: images[].imageManifest

Interestingly enough, if I omit the last two parameters (--query images[].imageManifest and --output text) then that command does succeed and returns a bit of JSON. So I tried to copy/paste that JSON by hand into an environment variable - specifically copying the section labeled "imageManifest." Then, using that environment variable (which I've named MANIFEST to stay consistent with the nomenclature that the guide uses), I tried to run this command:

aws ecr put-image --repository-name myappserver --image-tag new-tag --image-manifest "$MANIFEST"

However, that results in the following error message:

An error occurred (InvalidParameterException) when calling the PutImage operation: Invalid parameter at 'ImageManifest' failed to satisfy constraint: 'Invalid JSON syntax'

As far as I can tell, the JSON output that I copied into the put-image command is valid, despite that error. And I'm also confused why I'm unable to run the batch-get-image command with the provided arguments. What can I do to get these commands working, and to add tags to my image?

like image 344
soapergem Avatar asked May 09 '18 20:05

soapergem


2 Answers

You're getting a shell error (zsh) that says the wildcard expression images[].imageManifest didn't match any files on your local disk.

Try using quotes:

--query 'images[].imageManifest'

instead of this:

--query images[].imageManifest
like image 80
Ido Levy Avatar answered Nov 07 '22 02:11

Ido Levy


For those in windows, I needed to juggle a bit with the JSON

$MANIFEST = aws ecr batch-get-image --repository-name REPO --image-ids "imageDigest=sha256:DIGEST" --query 'images[].imageManifest' --output json 
$x = $MANIFEST | ConvertFrom-Json
$y = $x.replace('\n', ' ').replace("`n", " ")
aws ecr put-image --repository-name REPO --image-tag 2018.12 --image-manifest "$y"
 
like image 27
Roelant Avatar answered Nov 07 '22 04:11

Roelant