「ECR」をAWSマネジメントコンソールから作成する

ECR

ECR(Elastic Container Registry)とは、AWSのDockerレジストリサービスである。Dockerイメージをプライベートに管理し、IAMによるアクセス制御も可能である。 詳細は公式ドキュメントを参照すること。

ECRでは、Dockerイメージごとに、リポジトリを作成するだけで簡単にDockerイメージをプッシュまたはプルすることができるようになる。

リポジトリの作成

ECRのページへ遷移。

「今すぐ始める」ボタンをクリック。

f:id:tmknom:20180826122831p:plain

リポジトリ名を入力し、「次のステップ」ボタンをクリック。

f:id:tmknom:20180826122844p:plain

「完了」ボタンをクリック。使い方が表示されるが、これはあとから確認することが可能である。

f:id:tmknom:20180826122857p:plain f:id:tmknom:20180826122907p:plain

AWS CLI

試しに、DockerイメージをECRにpushして、AWS CLI確認で確認してみる。

describe-repositories

リポジトリの一覧取得。

$ aws ecr describe-repositories
{
    "repositories": [
        {
            "registryId": "123456789012",
            "repositoryName": "sample-ecr",
            "repositoryArn": "arn:aws:ecr:ap-northeast-1:123456789012:repository/sample-ecr",
            "createdAt": 1533702953.0,
            "repositoryUri": "123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/sample-ecr"
        }
    ]
}

list-images

Dockerイメージ一覧取得。タグとダイジェストしか取れないらしい。

$ aws ecr list-images --repository-name sample-ecr
{
    "imageIds": [
        {
            "imageTag": "latest",
            "imageDigest": "sha256:14062be7d16eb5327f7f5f36889c2fa409e02d5b2d6aadc4986bfe3b7b345123"
        }
    ]
}

describe-images

指定したDockerイメージの情報取得。イメージの詳細はこっちで取れる。

$ aws ecr describe-images --repository-name sample-ecr
{
    "imageDetails": [
        {
            "imageSizeInBytes": 82053737,
            "imageDigest": "sha256:14062be7d16eb5327f7f5f36889c2fa409e02d5b2d6aadc4986bfe3b7b345123",
            "imageTags": [
                "latest"
            ],
            "registryId": "123456789012",
            "repositoryName": "sample-ecr",
            "imagePushedAt": 1533704584.0
        }
    ]
}

get-repository-policy

デフォルトでは、リポジトリポリシーは未定義。

$ aws ecr get-repository-policy --repository-name sample-ecr

An error occurred (RepositoryPolicyNotFoundException) when calling the GetRepositoryPolicy operation: Repository policy does not exist for the repository with name 'sample-ecr' in the registry with id '123456789012'

get-lifecycle-policy-preview

デフォルトでは、ライフサイクルポリシーのプレビューは未定義。

$ aws ecr get-lifecycle-policy-preview --repository-name sample-ecr

An error occurred (LifecyclePolicyPreviewNotFoundException) when calling the GetLifecyclePolicyPreview operation: Lifecycle policy preview does not exist for the repository with name 'sample-ecr' in the registry with id '123456789012'

get-lifecycle-policy

デフォルトでは、ライフサイクルポリシーは未定義。

$ aws ecr get-lifecycle-policy --repository-name sample-ecr

An error occurred (LifecyclePolicyNotFoundException) when calling the GetLifecyclePolicy operation: Lifecycle policy does not exist for the repository with name 'sample-ecr' in the registry with id '123456789012'

get-login

3,000文字弱ぐらいのメッチャ長いトークンが払い出される。標準出力されたモノを、コンソールに貼り付けて実行するとECRにログインできる。

$ aws ecr get-login
docker login -u AWS -p xxxxxxxx -e none https://123456789012.dkr.ecr.ap-northeast-1.amazonaws.com

Dockerコマンド

ECRを前提とした、最低限のDockerコマンドのサンプル。

1. Docker クライアントを認証

$(aws ecr get-login --no-include-email --region ap-northeast-1)

2. イメージ構築

docker build -t 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/sample-ecr:latest .

3. イメージをプッシュ

docker push 123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/sample-ecr:latest