构建应用镜像

本节我们将构建Web、Dogs、Cats三个应用的Docker镜像,并将其上传到ECR,以便后面创建ECR Task Defination的时候使用。

下载应用代码:

curl 'https://static.us-east-1.prod.workshops.aws/public/657a7dd5-1773-49cc-aacd-903efff8577c/assets/code/workshop.zip' -o workshop.zip
unzip -o workshop.zip -d ~/environment/ecsworkshop

创建ECR仓库

aws ecr create-repository --repository-name cats --region ${AWS_REGION}
aws ecr create-repository --repository-name dogs --region ${AWS_REGION}
aws ecr create-repository --repository-name web --region ${AWS_REGION}

构建镜像并上传

构建cats应用镜像并上传到ECR:

cd ecsworkshop/cats
cat Dockerfile
docker build -t cats .
aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com

docker tag cats:latest $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/cats:latest
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/cats:latest

image-20231018092037538

构建dogs应用镜像并上传到ECR:

cd ecsworkshop/dogs
cat Dockerfile
docker build -t dogs .

docker tag dogs:latest $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/dogs:latest
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/dogs:latest

构建web应用镜像并上传到ECR:

cd ecsworkshop/web
cat Dockerfile
docker build -t web .

docker tag web:latest $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/web:latest
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/web:latest