imageをAWS ECRにビルド、プッシュする
概要
ECRについて、AWSの公式ページでは下記のように書かれてあります。
Amazon Elastic Container Registry (Amazon ECR) は、安全性、スケーラビリティ、信頼性を備えた AWS マネージドコンテナイメージレジストリサービスです。
簡単に言うと、ECRはimageを保存するサービスです。
特にAWSでコンテナ(ECS)を立ち上げる時、AWS ECRを使用することが多いです。
構成
前提条件
- AWSアカウント
- AWS CLI
- Docker/Podmanが使える状態
- コードをローカルにクローン済み
手順
ECRリポジトリ作成
imageをローカルでビルドしてECRにプッシュ
- 作成されたリポジトリをクリック
- 「プッシュコマンドを表示」をクリック
- ECRにログイン、imageビルド、imageにタグ付け、ECRにプッシュまでのコマンドが表示される
- docker buildコマンド「docker build -t ecs_cicd_repo_sample .」について
- Dockerfileがカレントパスに存在することが条件
- もしDockerfileが別のフォルダにある場合は、-f <フォルダまでのパス/Dockerfile>追加する必要がある
- Dockerfileがカレントパスに存在することが条件
- 実際にビルド
$ docker build -f docker/Dockerfile -t ecs_cicd_repo_sample . failed to fetch metadata: fork/exec /usr/local/lib/docker/cli-plugins/docker-buildx: no such file or directory DEPRECATED: The legacy builder is deprecated and will be removed in a future release. Install the buildx component to build images with BuildKit: https://docs.docker.com/go/buildx/ Sending build context to Docker daemon 93.7kB Step 1/8 : FROM public.ecr.aws/docker/library/node:22-alpine 22-alpine: Pulling from docker/library/node fe07684b16b8: Pull complete 65b9c193e6b7: Pull complete 826f8ad948ff: Pull complete cb37e5b9a0a1: Pull complete Digest: sha256:41e4389f3d988d2ed55392df4db1420ad048ae53324a8e2b7c6d19508288107e Status: Downloaded newer image for public.ecr.aws/docker/library/node:22-alpine ---> fb419a5d2f1d Step 2/8 : WORKDIR /usr/src/app ---> Running in 88a9c3843c6a ---> Removed intermediate container 88a9c3843c6a ---> 28a551b6fd0f Step 3/8 : COPY src/package*.json ./ ---> 57ffd361ab67 Step 4/8 : RUN npm install ---> Running in 2e957b057db1 up to date, audited 1 package in 532ms found 0 vulnerabilities ---> Removed intermediate container 2e957b057db1 ---> 8835ea4e8ff6 Step 5/8 : COPY src/ . ---> ef2aff6d2ecc Step 6/8 : RUN npm run build ---> Running in b96dbdcca3b3 > sample-app@1.0.0 build > echo 'No build step defined' No build step defined ---> Removed intermediate container b96dbdcca3b3 ---> 41bef90bcd47 Step 7/8 : EXPOSE 80 ---> Running in 47e9cfdaf75d ---> Removed intermediate container 47e9cfdaf75d ---> 43782973ce94 Step 8/8 : CMD [ "node", "index.js" ] ---> Running in 8f90bcca465d ---> Removed intermediate container 8f90bcca465d ---> 77c80fa23449 Successfully built 77c80fa23449 Successfully tagged ecs_cicd_repo_sample:latest $- 実際にECRログイン、プッシュ
- docker buildコマンド「docker build -t ecs_cicd_repo_sample .」について
$ aws --profile eddy-account ecr get-login-password --region
ap-northeast-1 | docker login --username AWS --password-stdin 77984681xxxx.dkr.ecr.ap-northeast-1.amazonaws.com
<略>
Login Succeeded
$ git rev-parse --short HEAD
b36afd8
$ docker tag ecs_cicd_repo_sample:latest 77984681xxxx.dkr.ecr.ap-northeast-1.amazonaws.com/ecs_cicd_repo_sample:latest
$ docker push 77984681xxxx.dkr.ecr.ap-northeast-1.amazonaws.com/ecs_cicd_repo_sample:latest
The push refers to repository [77984681xxxx.dkr.ecr.ap-northeast-1.amazonaws.com/ecs_cicd_repo_sample]
80d36157c6be: Pushed
cf59fec753fc: Pushed
968ce8a5ba85: Pushed
3a33190f2794: Pushed
0f20ca39eef4: Pushed
6a6cca838e27: Pushed
53fa260c3ba4: Pushed
126f43b2ffa5: Pushed
fd2758d7a50e: Pushed
latest: digest: sha256:936d5ff51dc72063978c20cb74f4c1ff097babf031f9a9ff1237e94af01a54ef size: 2197
$




