AWS: ECR (ElasticContainerRegistory) へdocker imageをpush

ECRでコンテナレジストリを作成し、ローカル環境でビルドしたdocker imageをECRにpushする

## ECRのUIでリポジトリを作成

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/600486/af44e201-2e8e-b2a9-780c-eb2734c61112.png)

pushコマンドを確認する

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/600486/13ae7099-5b41-a545-cf15-ed8625aafb77.png)

## ローカルからpushコマンドを実施するが。。。失敗

=> コマンドが成功するようにツールやトークンの設定を行う
- pip install --upgrade awscli
- pip install requests

```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/600486/f01d6162-52eb-82b8-3392-b10e1f1d290e.png)
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 575505200433.dkr.ecr.us-east-1.amazonaws.com
Traceback (most recent call last):
  File "/usr/bin/aws", line 19, in <module>
    import awscli.clidriver
  File "/usr/lib/python3/dist-packages/awscli/clidriver.py", line 17, in <module>
    import botocore.session
  File "/usr/lib/python3/dist-packages/botocore/session.py", line 28, in <module>
    import botocore.configloader
  File "/usr/lib/python3/dist-packages/botocore/configloader.py", line 19, in <module>
    from botocore.compat import six
  File "/usr/lib/python3/dist-packages/botocore/compat.py", line 25, in <module>
    from botocore.exceptions import MD5UnavailableError
  File "/usr/lib/python3/dist-packages/botocore/exceptions.py", line 15, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'
Error: Cannot perform an interactive login from a non TTY device
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ pip install requests
Requirement already satisfied: requests in /home/mozaki/miniconda3/lib/python3.11/site-packages (2.28.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/mozaki/miniconda3/lib/python3.11/site-packages (from requests) (2.0.4)
Requirement already satisfied: idna<4,>=2.5 in /home/mozaki/miniconda3/lib/python3.11/site-packages (from requests) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/mozaki/miniconda3/lib/python3.11/site-packages (from requests) (1.26.18)
Requirement already satisfied: certifi>=2017.4.17 in /home/mozaki/miniconda3/lib/python3.11/site-packages (from requests) (2024.7.4)

(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ 
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ pip install --upgrade awscli
Collecting awscli
  Downloading awscli-1.35.2-py3-none-any.whl.metadata (11 kB)

Collecting botocore==1.35.36 (from awscli)
  Downloading botocore-1.35.36-py3-none-any.whl.metadata (5.7 kB)
Collecting docutils<0.17,>=0.10 (from awscli)
  Downloading docutils-0.16-py2.py3-none-any.whl.metadata (2.7 kB)
Collecting s3transfer<0.11.0,>=0.10.0 (from awscli)
  Downloading s3transfer-0.10.3-py3-none-any.whl.metadata (1.7 kB)
Requirement already satisfied: PyYAML<6.1,>=3.10 in /home/mozaki/miniconda3/lib/python3.11/site-packages (from awscli) (6.0.1)
Collecting colorama<0.4.7,>=0.2.5 (from awscli)
  Downloading colorama-0.4.6-py2.py3-none-any.whl.metadata (17 kB)
Collecting rsa<4.8,>=3.1.2 (from awscli)
  Downloading rsa-4.7.2-py3-none-any.whl.metadata (3.6 kB)
Collecting jmespath<2.0.0,>=0.7.1 (from botocore==1.35.36->awscli)
  Downloading jmespath-1.0.1-py3-none-any.whl.metadata (7.6 kB)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /home/mozaki/miniconda3/lib/python3.11/site-packages (from botocore==1.35.36->awscli) (2.8.2)
Requirement already satisfied: urllib3!=2.2.0,<3,>=1.25.4 in /home/mozaki/miniconda3/lib/python3.11/site-packages (from botocore==1.35.36->awscli) (1.26.18)
Collecting pyasn1>=0.1.3 (from rsa<4.8,>=3.1.2->awscli)
  Downloading pyasn1-0.6.1-py3-none-any.whl.metadata (8.4 kB)
Requirement already satisfied: six>=1.5 in /home/mozaki/miniconda3/lib/python3.11/site-packages (from python-dateutil<3.0.0,>=2.1->botocore==1.35.36->awscli) (1.16.0)
Downloading awscli-1.35.2-py3-none-any.whl (4.5 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.5/4.5 MB 7.4 MB/s eta 0:00:00
Downloading botocore-1.35.36-py3-none-any.whl (12.6 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.6/12.6 MB 16.0 MB/s eta 0:00:00
Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Downloading docutils-0.16-py2.py3-none-any.whl (548 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 548.2/548.2 kB 30.7 MB/s eta 0:00:00
Downloading rsa-4.7.2-py3-none-any.whl (34 kB)
Downloading s3transfer-0.10.3-py3-none-any.whl (82 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 82.6/82.6 kB 23.3 MB/s eta 0:00:00
Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)
Downloading pyasn1-0.6.1-py3-none-any.whl (83 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 83.1/83.1 kB 25.1 MB/s eta 0:00:00
Installing collected packages: pyasn1, jmespath, docutils, colorama, rsa, botocore, s3transfer, awscli
Successfully installed awscli-1.35.2 botocore-1.35.36 colorama-0.4.6 docutils-0.16 jmespath-1.0.1 pyasn1-0.6.1 rsa-4.7.2 s3transfer-0.10.3
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ 
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ 
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ 
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 575505200433.dkr.ecr.us-east-1.amazonaws.com

An error occurred (UnrecognizedClientException) when calling the GetAuthorizationToken operation: The security token included in the request is invalid.
Error: Cannot perform an interactive login from a non TTY device
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ aws configure
AWS Access Key ID [****************SLMR]: 
```

### IAM上でユーザーの作成 & トークンの設定

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/600486/f4438887-96f0-cbb6-21c1-9a2e6b168228.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/600486/db2cde83-5d43-2e5b-b0e3-0c8f9aa4f089.png)


#### ローカル環境でIAMで作成したトークンを設定。。。。失敗

IAMで作成したユーザーにECRへアクセス許可がなかった

```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/600486/d2c17c45-c552-2301-5f5e-f1daf1a09840.png)
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ aws configure
AWS Access Key ID [****************SLMR]: AKIAYL7WIOUY7RPPKFMJ
AWS Secret Access Key [****************5DjH]: eIFctxxxxxxxxxxxxxxxxxbEqRNLOKh0
Default region name [ap-northeast-1]: us-east-1
Default output format [None]: json
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ 
```

#### IAM上でユーザーにECRへのアクセス許可を追加

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/600486/39e006ce-f8f7-08db-1d80-e4d980e42318.png)


#### 再度ローカルからECRへのアクセス確認....成功

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/600486/ede7e1cc-cb88-816c-a564-74025bd2aafd.png)


### ECRのpushコマンドを実行し、ECRへdocker imageのpush完了


```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/600486/f0cda441-b68e-5ebb-f34b-beaf8d7924c8.png)
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ docker tag coworkeing-checkin 575505200433.dkr.ecr.us-east-1.amazonaws.com/coworking-checkin:
latest
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ docker images | grep checkin
575505200433.dkr.ecr.us-east-1.amazonaws.com/coworking-checkin           latest                                             2a2893eeacb2   27 minutes ago   509MB
coworkeing-checkin                                                       latest                                             2a2893eeacb2   27 minutes ago   509MB
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$ docker push 575505200433.dkr.ecr.us-east-1.amazonaws.com/coworking-checkin:latest
The push refers to repository [575505200433.dkr.ecr.us-east-1.amazonaws.com/coworking-checkin]
32bc5e962f6a: Pushed 
7f974888ae13: Pushed 
8dbcba59aabb: Pushed 
4aebad7b0ec9: Pushed 
bd39ee569927: Pushed 
d883d146c0aa: Pushed 
c6cb9edd6fa5: Pushed 
dbb48723286e: Pushed 
c5321f7f53ff: Pushed 
df6c1b185b95: Pushed 
b23fedba7dbd: Pushed 
ae2d55769c5e: Pushed 
e2ef8a51359d: Pushed 
latest: digest: sha256:93966de3fcd924c9b3a11c69236caa8d3869f4acd4cabfa8a2a1d5797260d69f size: 3045
(base) mozaki@yoshitsune:~/004_HDD/001_CODE/901_UDACITY_AWS/cd12355-microservices-kubernetes-cloud-devops/lesson-2-containers/exercises/2_images-and-containers/starter$
```