1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS ECRとローカル環境間でイメージをpull&pushする方法

Posted at

みなさん、こんにちは!

AWS ECR は、コンテナイメージを管理するリポジトリです。
Gitリポジトリと同じように、イメージをpullしてローカル環境で利用したり、ローカル環境のイメージをpushしてECS等で利用することができます。

本記事では、AWS ECR のプライベートレジストリとローカル環境の間でコンテナイメージをpull&pushする方法についてご紹介します。

準備

AWS ECR とローカル環境で通信するにあたり、AWS CLI のインストールと認証情報の設定を行う必要があります。

AWS CLI のインストールと認証情報の設定については、通常の場合と IAM Identity Center の場合で方法が異なります。以下を参考に設定を行ってください。

通常の場合

【AWS】aws cliの設定方法

IAM Identity Center の場合

AWS CLIをAWS IAM Identity Center(SSO)で認証させるには? | DevelopersIO

また、Dockerコマンドを使うのでDockerのインストールも必要です。

レジストリ認証

認証情報の設定まで完了したら、以下のコマンドでプライベートレジストリ認証を行います。

aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com

account_idregionはご自身のものに置き換えてください。

以下のような表示が出れば成功です。

WARNING! Your password will be stored unencrypted in /home/tani/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

イメージのpull

ECR からローカル環境にイメージをpullする場合、以下のコマンドを実行します。

docker pull <account_id>.dkr.ecr.<region>.amazonaws.com/<repository>:<tag>

repositoryは対象リポジトリ名、tagには対象イメージのタグを指定します。

以下のようにpullが実行され、ローカル環境にイメージがダウンロードされます。

dev-km_loader: Pulling from reqgpt-repo
...
417f18bbb763: Pull complete 
eb6b2be00db9: Pull complete 
Digest: sha256:a1f15e15504ca3c31f5b45feebc8c45534b279807aab94303af48b86372563ef
Status: Downloaded newer image for xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/reqgpt-repo:dev-km_loader
xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/reqgpt-repo:dev-km_loader

pullしたイメージは下記コマンドで確認できます。

docker images

イメージからコンテナを起動する場合、以下のように通常通り起動すればOKです。

docker run <image_id>

イメージのpush

ローカル環境から ECR にイメージをpushする場合、以下のコマンドを実行します。

docker tag <image_id> <account_id>.dkr.ecr.<region>.amazonaws.com/<repository>:<tag>
docker push <account_id>.dkr.ecr.<region>.amazonaws.com/<repository>:<tag>

docker tagで対象のイメージにタグ(push先のリポジトリとタグの組み合わせ)を付け、それをdocker pushでpushします。一発でpushする方法はないので、タグを付けてpush、という二段階の操作が必要になります。

実行例:

$ docker tag 6ca355ce0951 xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/reqgpt-repo:test_loader
$ docker push xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/reqgpt-repo:test_loader

さいごに

いざ ECR のプライベートリポジトリとローカル環境でイメージを受け渡ししようと思ったとき、やり方に少しクセがあって詰まってしまいました。
本記事が同じような方の参考になれば幸いです。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?