{ECR}AWS CLI での Amazon ECR の使用


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

https://study-infra.com/ecr-begginer/


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version


-- 1.2 jqインストール
sudo yum -y install jq


-- 1.3 Dockerインストール
sudo yum update -y
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user

別端末を起動
docker info


-- 2. Docker イメージを作成する

vim 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

 

docker build -t helloworld .

docker images --filter reference=helloworld

docker run -t -i -p 80:80 helloworld

 

-- 3. デフォルトレジストリに対して認証する
aws ecr get-login-password --region ap-northeast-1 | \
docker login --username AWS --password-stdin 999999999999.dkr.ecr.ap-northeast-1.amazonaws.com


-- 4. レポジトリを作成する

※ レポジトリ名はタグ名と一致させる必要がある

aws ecr create-repository \
--repository-name helloworld \
--image-tag-mutability MUTABLE \
--image-scanning-configuration scanOnPush=false


aws ecr describe-repositories

 

-- 5. イメージを Amazon ECR にプッシュする

docker images
docker tag helloworld:latest 999999999999.dkr.ecr.ap-northeast-1.amazonaws.com/helloworld:latest

aws ecr list-images \
--repository-name helloworld


docker push 999999999999.dkr.ecr.ap-northeast-1.amazonaws.com/helloworld:latest

aws ecr list-images \
--repository-name helloworld

 


-- 6. Amazon ECR からイメージをプルする

docker images
docker image rmi 111111111111 -f
docker images

docker pull 999999999999.dkr.ecr.ap-northeast-1.amazonaws.com/helloworld:latest
docker images

-- 7. イメージを削除する
aws ecr list-images \
--repository-name helloworld

aws ecr batch-delete-image \
--repository-name helloworld \
--image-ids imageTag=latest

aws ecr list-images \
--repository-name helloworld


-- 8. クリーンアップ

aws ecr delete-repository \
--repository-name helloworld \
--force

aws ecr describe-repositories