🪦

ECRを試す

2023/12/24に公開

ECR作成

参考

https://docs.aws.amazon.com/ja_jp/AmazonECR/latest/userguide/what-is-ecr.html
https://docs.aws.amazon.com/ja_jp/AmazonECR/latest/userguide/getting-started-cli.html

リポジトリ作成

作成前の状態

$ aws ecr describe-repositories
{
    "repositories": []
}

リポジトリ名を指定するだけで、リポジトリを作成することができます。

$ aws ecr create-repository --repository-name <repository_name>
{
    "repository": {
        "repositoryUri": "<aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>",
        "imageScanningConfiguration": {
            "scanOnPush": false
        },
        "encryptionConfiguration": {
            "encryptionType": "AES256"
        },
        "registryId": "<aws_account_id>",
        "imageTagMutability": "MUTABLE",
        "repositoryArn": "arn:aws:ecr:<region>:<aws_account_id>:repository/<repository_name>",
        "repositoryName": "<repository_name>",
        "createdAt": 1703277662.0
    }
}

ログイン

出力したパスワードをdocker コマンドの標準入力から入れる使い方もできますが、
自分の環境では aws-cli と docker は異なる環境に入れているので、ここでは一時的にファイルに保存します。

$ aws ecr get-login-password > ~/work/ecr-login-password

ログインします。

$ cat /export/home/docker/work/ecr-login-password | sudo docker login -u AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

一時的に保存したファイルは以後不要なので削除します。

$ rm /export/home/docker/work/ecr-login-password

イメージのpush

テストとして、みんな大好き hello-world イメージを利用します。
タグ付けして push します。

$ sudo docker tag hello-world:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:test

$ sudo docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:test
The push refers to repository [<aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>]
12660636fe55: Pushed
test: digest: sha256:a8ea96bb64d60208d6a56712042d1cf58aa4a7d3751b897b9320b0813c81cbb4 size: 524

push後、イメージを確認します。

$ aws ecr list-images --repository-name <repository_name>
{
    "imageIds": [
        {
            "imageTag": "test",
            "imageDigest": "sha256:a8ea96bb64d60208d6a56712042d1cf58aa4a7d3751b897b9320b0813c81cbb4"
        }
    ]
}

$ aws ecr describe-images --repository-name <repository_name>
{
    "imageDetails": [
        {
            "artifactMediaType": "application/vnd.docker.container.image.v1+json",
            "imageSizeInBytes": 3776,
            "imageDigest": "sha256:a8ea96bb64d60208d6a56712042d1cf58aa4a7d3751b897b9320b0813c81cbb4",
            "imageManifestMediaType": "application/vnd.docker.distribution.manifest.v2+json",
            "imageTags": [
                "test"
            ],
            "registryId": "<aws_account_id>",
            "repositoryName": "<repository_name>",
            "imagePushedAt": 1703280057.0
        }
    ]
}

イメージの pull

テストのため、ローカルの既存のイメージを削除しておきます。

$ sudo docker rmi <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:test
Untagged: <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:test
Untagged: <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>@sha256:a8ea96bb64d60208d6a56712042d1cf58aa4a7d3751b897b9320b0813c81cbb4

$ sudo docker rmi hello-world:latest
Untagged: hello-world:latest
Untagged: hello-world@sha256:ac69084025c660510933cca701f615283cdbb3aa0963188770b54c31c8962493

さきほど push したイメージを pull します。

$ sudo docker pull <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:test
test: Pulling from <repository_name>
Digest: sha256:a8ea96bb64d60208d6a56712042d1cf58aa4a7d3751b897b9320b0813c81cbb4
Status: Downloaded newer image for <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:test
<aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:test

実行してみます。

$ sudo docker run --rm <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>:test

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

イメージの削除

push したイメージをリポジトリから削除してみます。

$ aws ecr batch-delete-image --repository-name <repository_name> --image-ids imageTag=test
{
    "failures": [],
    "imageIds": [
        {
            "imageTag": "test",
            "imageDigest": "sha256:a8ea96bb64d60208d6a56712042d1cf58aa4a7d3751b897b9320b0813c81cbb4"
        }
    ]
}

削除後の確認

$ aws ecr list-images --repository-name <repository_name>
{
    "imageIds": []
}

リポジトリの削除

作成したリポジトリを削除してみます。

$ aws ecr delete-repository --repository-name <repository_name>
{
    "repository": {
        "repositoryUri": "<aws_account_id>.dkr.ecr.<region>.amazonaws.com/<repository_name>",
        "registryId": "<aws_account_id>",
        "imageTagMutability": "MUTABLE",
        "repositoryArn": "arn:aws:ecr:<region>:<aws_account_id>:repository/<repository_name>",
        "repositoryName": "<repository_name>",
        "createdAt": 1703277662.0
    }
}

参考: CloudFormation テンプレート

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  RepositoryName:
    Type: String
  ENV:
    Type: String
Resources:
  MyRepository:
    Type: AWS::ECR::Repository
    Properties:
      RepositoryName: !Ref RepositoryName
      ImageScanningConfiguration:
        ScanOnPush: false
      EncryptionConfiguration:
        EncryptionType: "AES256"
      ImageTagMutability: "MUTABLE"
      Tags:
        - Key: Name
          Value: !Ref RepositoryName
        - Key: ENV
          Value: !Ref ENV
GitHubで編集を提案

Discussion