EC2インスタンス(Amazon Linux 2)を作成して、Docker Image を作成して、ECR に push したメモ。
- Docker をインストール/起動する
$ sudo yum -y update $ sudo yum -y install docker $ sudo systemctl start docker
- Docker イメージを作成する
$ mkdir -p docker/nginx-test $ cd docker/nginx-test $ cat > new_index.html << EOF <p>Deployed Nginx content using Docker container!!</p> EOF $ cat > Dockerfile << 'EOF' ARG VERSION=2 FROM amazonlinux:$VERSION MAINTAINER yoheia RUN echo "now building..." RUN amazon-linux-extras install nginx1 -y ADD ./new_index.html /usr/share/nginx/html/index.html EXPOSE 80 CMD ["/usr/sbin/nginx", "-g", "daemon off;"] EOF $ sudo docker image build -t nginx-test .
- Docker コンテナを実行する
$ sudo docker container run -d -p 8080:80 nginx-test:latest $ sudo docker ps -a
- 動作確認
$ curl http://10.0.0.48:8080/ <p>Deployed Nginx content using Docker container!!</p>
- ECR にログインする
$ export REGION_NAME=ap-northeast-1 $ aws ecr get-login --region ${REGION_NAME} --no-include-email docker login -u AWS -p ... $ sudo docker login -u AWS -p ... Login Succeeded
- ECR にリポジトリ作成
$ export REPOSITORY_NAME=sample-repo $ aws ecr create-repository --repository-name ${REPOSITORY_NAME} --region ${REGION_NAME} { "repository": { "repositoryUri": "123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/sample-repo", "imageScanningConfiguration": { "scanOnPush": false }, "encryptionConfiguration": { "encryptionType": "AES256" }, "registryId": "123456789012", "imageTagMutability": "MUTABLE", "repositoryArn": "arn:aws:ecr:ap-northeast-1:123456789012:repository/sample-repo", "repositoryName": "sample-repo", "createdAt": 1631135742.0 } }
$ export REPOSITORY_URI=123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/sample-repo
- Docker image を ECR に push する
$ sudo docker build -t ${REPOSITORY_URI}:latest . $ sudo docker push ${REPOSITORY_URI}:latest
- ECR に push された image を確認する
$ aws ecr list-images --repository-name ${REPOSITORY_NAME} { "imageIds": [ { "imageTag": "latest", "imageDigest": "sha256:4e3d3e2c7bfc1e3258596bc142f23b9d483acaac6a3c2abefd94a73d788164fb" } ] }