Push an image whose name contains slashes
Asked by mgarcia:

As image names can be specified in AWS ECR with slashes, for example, "my-group/my-image-name" I would like to specify this information in a pipe. So it would look like:

```
- step: 
    name: Build Dockerfile and push it to ECR
    script:
      - docker build -t my-group/my-image-name .
      - pipe: atlassian/aws-ecr-push-image:1.5.0
        variables:
          AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
          AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
          AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
          IMAGE_NAME: my-group/my-image-name
          DEBUG: "true"
```

When I've tried this, the following error was thrown:

```
DEBUG: http://host.docker.internal:2375 "POST /v1.41/images/xxxx.dkr.ecr.xxxx.amazonaws.com/my-group/my-image-name/push?tag=latest HTTP/1.1" 200 None
INFO: {'status': 'The push refers to repository [xxxx.dkr.ecr.xxxx.amazonaws.com/my-group/my-image-name]'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '6816afbbb3be'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '16fdcefba9b2'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': 'ea44e3716f74'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '8607c8c34ac2'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': 'c9109473da10'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '16c770f32328'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '866fb7fffb5e'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '25b3fd43616d'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '957cc3cb8f33'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '31d8693e83b2'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '640439866c2f'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '5d2a276f8b89'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '058670b42da8'}
INFO: {'status': 'Preparing', 'progressDetail': {}, 'id': '9f54eef41275'}
INFO: {'status': 'Waiting', 'progressDetail': {}, 'id': '16c770f32328'}
INFO: {'status': 'Waiting', 'progressDetail': {}, 'id': '866fb7fffb5e'}
INFO: {'status': 'Waiting', 'progressDetail': {}, 'id': '25b3fd43616d'}
INFO: {'status': 'Waiting', 'progressDetail': {}, 'id': '957cc3cb8f33'}
INFO: {'status': 'Waiting', 'progressDetail': {}, 'id': '31d8693e83b2'}
INFO: {'status': 'Waiting', 'progressDetail': {}, 'id': '640439866c2f'}
INFO: {'status': 'Waiting', 'progressDetail': {}, 'id': '5d2a276f8b89'}
INFO: {'status': 'Waiting', 'progressDetail': {}, 'id': '058670b42da8'}
INFO: {'status': 'Waiting', 'progressDetail': {}, 'id': '9f54eef41275'}
INFO: {'status': 'Retrying in 5 seconds', 'progressDetail': {}, 'id': '8607c8c34ac2'}
INFO: {'status': 'Retrying in 5 seconds', 'progressDetail': {}, 'id': '6816afbbb3be'}
```

After several attempts, the error is:

```
INFO: {'errorDetail': {'message': 'EOF'}, 'error': 'EOF'}
? Docker push error: EOF
```

I have tried it without the slash, i.e. "my-image-name", it worked properly. Also, I've tried it manually, i.e. doing it with the AWS CLI, and it worked with the slash in the name.

Am I doing something wrong?

Thanks!

**Update:**

I have tried the name with an underscore an it's failing too.

Also, I've forked the project: <https://bitbucket.org/atlassian/aws-ecr-push-image/src/master/> and I've tried the pipe locally, building the Dockerfile and running a container like:

```
docker run -it -e AWS_ACCESS_KEY_ID=xxx -e AWS_DEFAULT_REGION=xxx -e AWS_SECRET_ACCESS_KEY=xxxx -e IMAGE_NAME=my-group/my-image-name -w $(pwd) -v $(pwd):$(pwd) -v /var/run/docker.sock:/var/run/docker.sock aws-ecr-pipe
```

And this has worked.

So my suspicion is that maybe it's related to how Bitbucket is managing the variable "IMAGE_NAME".


Answers:
author: oddholst
body: For others with similar symptom, but correct naming, check your credentials on AWS. I got the exact same symptom (and therefore found this topic) and it was because I had not setup the policy for bitbucket service user correctly.

```

{
  "Version": "2012-10-17",
  "Statement": [ 
    { 
      "Effect": "Allow",
      "Action": [ 
        "ecr:BatchCheckLayerAvailability",
        "ecr:BatchGetImage",
       ?"ecr:CompleteLayerUpload",
        "ecr:DescribeImages",
       ?"ecr:DescribeRepositories",
        "ecr:GetAuthorizationToken", 
        "ecr:GetDownloadUrlForLayer",
        "ecr:GetRepositoryPolicy",
        "ecr:InitiateLayerUpload",
        "ecr:ListImages",
        "ecr:PutImage",
        "ecr:UploadLayerPart"
      ],
      "Resource": "<accountnumber>.dkr.ecr.<your-region>.amazonaws.com/*"
    }
  ]
}
```

comments: author: Vimal
body: This solved my issue , thank you !!!


author: Nic Brough -Adaptavist-
body: Slashes are control characters, indicating escaping or directories.

You will need to escape them in the name of the file if you insist on using them, or adopt the good practice of not using them in file names.

comments: author: Igor S.
body: Hi Nic,

I've absolutely the same issue as described above.

```
- step: 
    name: Build Dockerfile and push it to ECR
    script:
      - docker build -t my-group/my-image-name .
      - pipe: atlassian/aws-ecr-push-image:1.5.0
        variables:
          IMAGE_NAME: my-group/my-image-name
          DEBUG: "true"
```

Can you please explain how to *escape* the image name like ***my-group/my-image-name***? When I tried to add a backslash in the repository variable the docker build failed with the following error:

```
invalid argument "*my-group* *\\\\/* *my-image-name*" for "-t, --tag" flag: invalid reference format
```

Thanks,  
Igor


author: Dimitry Kudryavtsev
body: [@Igor Stoyanov](/t5/user/viewprofilepage/user-id/4550492) does the ECR need to be created before you can push images to it?

comments: author: Igor Stoyanov
body: [@Dimitry Kudryavtsev](/t5/user/viewprofilepage/user-id/5141261) hi. According to pipe README:

Prerequisites {#toc-hId--494695143}
-----------------------------------

To use this pipe you should have a IAM user configured with programmatic access or Web Identity Provider (OIDC) role, with the necessary permissions to push docker images to your ECR repository. You also need to set up an ECR container registry if you don't already have on. Here is a [AWS ECR Getting started](https://docs.aws.amazon.com/AmazonECR/latest/userguide/ECR_GetStarted.html) guide from AWS on how to set up a new registry.

So the answer is **yes**.

Regards, Igor


author: Igor Stoyanov
body: [@mgarcia](/t5/user/viewprofilepage/user-id/4656402) hi. So i tested this case and found no issues.  

You can find my [config here](https://bitbucket.org/atlassian/aws-ecr-push-image/src/32eeb2b26fed1374f034883a2eceae7f86f10b13/bitbucket-pipelines.yml).

As you can see i have IMAGE_NAME **test/bbci-pipes-test-ecr-${BITBUCKET_BUILD_NUMBER}** which is equal for my build **test/bbci-pipes-test-ecr-186**

I successfully pushed image to ECR.  

The only case when i received your error: *INFO: {'errorDetail': {'message': 'EOF'}, 'error': 'EOF'}\\x1b\[0m\\n\\x1b\[31m? Docker push error: EOF\\x1b\[0m\\n"*   
was only when docker image name not the same as provided in pipe variables.

In example you built **test/bbci-pipes-test-ecr-186** but tried to push to **123456789.dkr.ecr.us-east-1.amazonaws.com/bbci-pipes-test-ecr-186** (without **test/** group)  

Also our team updated tests to incude case with slashes and will soon release a new patch to include these updates.

<br />

Regards, Igor

![Screenshot 2022-09-19 at 18.08.40.png](https://community.atlassian.com/t5/image/serverpage/image-id/216627i0970009D7C2B1C33/image-size/large?v=v2&px=999 "Screenshot 2022-09-19 at 18.08.40.png")![Screenshot 2022-09-19 at 18.06.15.png](https://community.atlassian.com/t5/image/serverpage/image-id/216628i0604BA9E8CAB0A9F/image-size/large?v=v2&px=999 "Screenshot 2022-09-19 at 18.06.15.png")

comments: author: Igor Stoyanov
body: Added config directly:

```
script:  - IMAGE_NAME="test/bbci-pipes-test-ecr-${BITBUCKET_BUILD_NUMBER}"  - docker build -t ${IMAGE_NAME} .  - docker image inspect ${IMAGE_NAME}  - pipe: atlassian/aws-ecr-push-image:1.6.0    variables:      AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID      AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY      AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION      IMAGE_NAME: ${IMAGE_NAME}services:  - docker

```


author: ebarcelos
body: [@mgarcia](/t5/user/viewprofilepage/user-id/4656402),

I was experiencing the same problem. Here's how you can solve it --

Instead of:

```
IMAGE_NAME: my-group/my-image-name
```

use:

```
IMAGE_NAME: my-image-name
```

**Explanation:** AWS ECR may contain several repos for your different container images and as far as I'm concerned, ECR repos are not meant to be act as a group like you intended.

Let me know if this helps.


author: Tony Edwards
body: I received the same error when "-" was in the image name.

comments: author: Tony Edwards
body: And you can't add underscore. Just tested both. I would feel better if unit tests checked for the common naming convention special characters.


author: Tony Edwards
body: Disregard. Error message was the same but the issue was I did not have the Elastic Container Registry created yet.


author: Igor Stoyanov
body: [@mgarcia](/t5/user/viewprofilepage/user-id/4656402), [@Igor S_](/t5/user/viewprofilepage/user-id/3728482) hi. We will investigate this issue and notify you.

Regards, Igor.

comments: author: mgarcia
body: Thank you [@Igor Stoyanov](/t5/user/viewprofilepage/user-id/4550492) ;)


Tags: aws

bitbucket-pipelines

cloud