LoginSignup
2
1

More than 1 year has passed since last update.

AWS CLIを使ってECRに自前Dockerイメージを登録する

Last updated at Posted at 2020-09-19

やりたいこと

ローカルで作成したDockerイメージをAWS CLIを使ってECRにプッシュする

前提

・AWS CLI バージョン2
・Dockerイメージ作成済

登録手順

ここに記載したのは下記公式サイトを自分なりにまとめたものです。

前準備

  • aws configure
    aws cliを利用するときにaws configureを実施して、認証情報を設定しておく必要があります。
    これはAWS CLIをセットアップする方法で、下記4つの情報の入力を求められます。

    • アクセスキーID
    • シークレットアクセスキー
    • AWSリージョン
    • 出力形式

configureするとき、下記のように--profileを指定しておくことで設定情報を再利用できます。
ここでは、prf-nameという名前にしました。

aws configure --profile prf-name

これを設定しておくとawsコマンドを使用するときに下記のような感じで--profileで作成した設定情報を指定してやれば、自分がどこにアクセスしたいのか簡単にコマンドに教えることができるわけです。

aws s3 ls --profile prf-name

--profileの指定がないと下記のように認証情報を特定することができないのでまずはaws configureしてね、と注意されます

Unable to locate credentials. You can configure credentials by running "aws configure"

作成したprofileのリストを確認したい場合は下記を実行します。設定した名前を忘れてしまったときに便利です。

aws configure list-profiles

Amazon ECRリポジトリを作成する

aws ecr create-repository --repository-name [your-repository-name] --region [region] --profile [prf-name]

your-repository-name: 好きなリポジトリ名
region: リージョン名(例:ap-northeast-1)
prf-name:profile名

公式サイトには記載がないですが、--profileを記載していないと、さきほどの認証エラーメッセージが表示されてしまいます。

イメージにタグをつけ、Amazon ECRにプッシュする

1. Amazon ECRリポジトリを作成する
ここでは"hello-repository"を作成します。

aws ecr create-repository --repository-name hello-repository --region [region]

region: リージョン名(例:ap-northeast-1)
出力:

{
    "repository": {
        "registryId": "aws_account_id",
        "repositoryName": "hello-repository",
        "repositoryArn": "arn:aws:ecr:region:aws_account_id:repository/hello-repository",
        "createdAt": 1505337806.0,
        "repositoryUri": "aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository"
    }
}

2.前のステップの repositoryUri の値で hello-world イメージにタグを付けます。

docker tag hello-world aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository

3.aws ecr get-login-password コマンドを実行します

aws ecr get-login-password --profile [prf-name] | docker login --username AWS --password-stdin [aws_account_id].dkr.ecr.[region].amazonaws.com

prf-name:profile名
aws_account_id:AWS アカウントを一意に識別する 12 桁の数値
region: リージョン名(例:ap-northeast-1)

出力:

Login Succeeded

4.前のステップの値 repositoryUri を使用して、Amazon ECR にイメージをプッシュします。

docker push aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository

イメージタグをつけたい場合は下記のように指定します。

docker push aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository:v1.0.0

5.プッシュしたイメージの確認
list-imagesを使って、プッシュしたイメージを確認

aws ecr list-images --repository-name hello-repository --region [region] --profile [prf-name]

prf-name:profile名
region: リージョン名(例:ap-northeast-1)

2
1
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
2
1