ECSで使用するコンテナイメージを作成し、ECRにプッシュするところまで行います。
以下の公式ドキュメントどおりの手順です。
Creating a container image for use on Amazon ECS - Amazon Elastic Container Service
Amazon ECS uses Docker images in task definitions to launch containers. Docker is a technology that provides the tools for you to build, run, test, and deploy d...
Dockerイメージを作成する
最初にDockerイメージを格納するディレクトリを作成します。
そして、同ディレクトリに移動後、Dockerfileを作成します。
$ mkdir ecs-image
$ cd ecs-image
$ touch Dockerfile
Dockerfileを以下のとおり編集します。
FROM ubuntu:18.04
# Install dependencies
RUN apt-get update && \
apt-get -y install apache2
# Install apache and write hello world message
RUN echo 'Hello World!' > /var/www/html/index.html
# Configure apache
RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh && \
echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh && \
echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh && \
echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh && \
chmod 755 /root/run_apache.sh
EXPOSE 80
CMD /root/run_apache.sh
上記では、DockerHubからUbuntu18.04イメージを利用し、以下を行なっています。
- apt-getコマンドでパッケージを最新にした上で、apache2をインストール
- apacheに接続した際に表示されるホーム画面のファイル(index.html)を準備
- apache起動に必要なスクリプトファイル(run_apache.sh)を作成
- ポート80公開
- 作成したスクリプトファイルを実行
DockerfileからDockerイメージをビルドします。
$ docker build -t hello-world .
$ docker images --filter reference=hello-world
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 4266b1c5ae31 22 seconds ago 201MB
Dockerイメージを実行します。
$ docker run -t -i -p 80:80 hello-world
xxx: apache2: Could not reliably determine the server's fully qualified domain name,
using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
ブラウザからhttp://localhostを確認すると、「Hello World!」が確認できます。
確認後、Ctrl+CでDockerコンテナを中止します。
DockerイメージをECRにプッシュ
Dockerイメージを格納するECRリポジトリを作成します。
$ aws ecr create-repository --repository-name hello-world
{
"repository": {
"repositoryArn": "arn:aws:ecr:ap-northeast-1:xx:repository/hello-world",
"registryId": "xx",
"repositoryName": "hello-world",
"repositoryUri": "xx.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world",
"createdAt": "2022-06-13T19:37:18+09:00",
"imageTagMutability": "MUTABLE",
"imageScanningConfiguration": {
"scanOnPush": false
},
"encryptionConfiguration": {
"encryptionType": "AES256"
}
}
}
ECRにプッシュする前に、Dockerイメージに上記のrepositoryUri値のタグをつけます。
このタグを付けなければECRにプッシュできません。
$ docker tag hello-world {accountId}.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world
ECRレジストリに対してDockerを認証します。
$ aws ecr get-login-password | \
docker login --username AWS \
--password-stdin {accountId}.dkr.ecr.ap-northeast-1.amazonaws.com
Login Succeeded
最後に、DockerイメージをECRにプッシュして完了です。
$ docker push {accountId}.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world
Using default tag: latest
The push refers to repository [xxx.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world]
6d5a3288e7e1: Pushed
b6ee56c9cbfc: Pushed
ac6d8d8c934f: Pushed
95129a5fe07e: Pushed
latest: digest: sha256:xxx
size: 1155
なぜECRにプッシュするイメージをrepositoryUri値でタグ付けする必要があるのか
ECRにDockerイメージをプッシュする際、事前に以下のタグ付けを行いました。
$ docker tag hello-world {accountId}.dkr.ecr.ap-northeast-1.amazonaws.com/hello-world
なぜこのタグ付けが必要かというと、docker push する際の決まり事のためです。
docker pushは、ECRへ行うには以下の形式で行います。
docker push {レジストリ名}/{リポジトリ名}[:{タグ名}]
そのため、docker push する際、Dockerイメージの名前は利用者がプッシュ先のリポジトリ名と無関係に設定できるものではなく、上記の形式に合わせて名前付けしなければなりません。
つまり、Dockerイメージ名は、プッシュ先の「{レジストリ名}/{リポジトリ名}」である必要があります。