AWSのECR(Container Registry)にHello, WorldなDockerイメージを置いてみる

Amazon Elastic Container Registry(ECR)を初めて触ってみます。

(前回Google Container Registryを触ってました。)

Dockerコンテナイメージ作成

以下のような適当なDockerfileを作成します。

FROM ubuntu:20.04
RUN echo "Hello, world!" > /hello.txt

コンテナイメージをビルドします。Dockerfile のあるディレクトリで以下のコマンドでビルドできます。

$ docker build -t hello-world .

作成したコンテナイメージは、docker imagesコマンドで見えるようになります。

$ docker images
REPOSITORY                                                      TAG                  IMAGE ID            CREATED             SIZE
hello-world                                                     latest               a94c81bcd9f5        10 minutes ago      72.9MB

試しにコンテナイメージを実行してみます。

$ docker run --rm hello-world cat /hello.txt
Hello, world!

AWSのECR(Container Registry)にpush

まずはcreate-repositoryします。

$ aws ecr create-repository --repository-name hello-world

aws ecr describe-repositoriesコマンドでリポジトリに作成できているのが確認できます。

$ aws ecr describe-repositories --repository-names hello-world
{
    "repositories": [
        {
            "repositoryArn": "arn:aws:ecr:ap-northeast-1:xxxxxxxxxxxx:repository/hello-world",
            "registryId": "xxxxxxxxxxxx",
            "repositoryName": "hello-world",
            "repositoryUri": "xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world",
            "createdAt": 1618978639.0,
            "imageTagMutability": "MUTABLE",
            "imageScanningConfiguration": {
                "scanOnPush": false
            },
            "encryptionConfiguration": {
                "encryptionType": "AES256"
            }
        }
    ]
}

aws ecr list-imagesコマンドで見ると、まだイメージがないのがわかります。

$ aws ecr list-images --repository-name hello-world
{
    "imageIds": []
}

pushするために、タグを付けます。新しいタグの名前は ACCOUNT_ID.dkr.ecr.ap-northeast-1.amazonaws.com/NAME というフォーマットです。ACCOUNT_IDは12桁の数字が入ります。

$ docker tag hello-world xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world

docker imagesコマンドでタグ付けした結果を確認できます。

$ docker images
REPOSITORY                                                      TAG                  IMAGE ID            CREATED             SIZE
xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world   latest               a94c81bcd9f5        37 minutes ago      72.9MB
hello-world                                                     latest               a94c81bcd9f5        37 minutes ago      72.9MB

このコンテナイメージをECRにアップロードします。

$ $(aws ecr get-login --no-include-email)
$ docker push xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world

aws ecr get-login --no-include-emailコマンドは docker login -u AWS -p ........ という出力をするので、それをそのまま実行すればdocker loginできるようです。

pushできたことは、aws ecr list-imagesコマンドで確認できます。

$ aws ecr list-images --repository-name hello-world
{
    "imageIds": [
        {
            "imageDigest": "sha256:7ad2413ef0a6c81e07ab1fc21e1fa2253f78c3ecae3126f539e96cfab2d624f6",
            "imageTag": "latest"
        }
    ]
}

AWSのECR(Container Registry)からpull

別の環境からpullにするには改めてdocker loginが必要です。

$ $(aws ecr get-login --no-include-email)

ログインできていれば、以下のコマンドで、pullして実行を確認できます。

$ docker run --rm xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world cat /hello.txt
Hello, world!

バージョン情報

$ docker --version
Docker version 19.03.8, build afacb8b7f0
$ aws --version
aws-cli/1.18.209 Python/3.8.5 Linux/5.4.0-1036-gcp botocore/1.19.49

aws-cliのバージョンが2ではdocker loginのコマンドが違うようです。