sh 构建Docker镜像,将其推送到AWS EC2 Container Registry,然后将其部署到AWS Elastic Beanstalk

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 构建Docker镜像,将其推送到AWS EC2 Container Registry,然后将其部署到AWS Elastic Beanstalk相关的知识,希望对你有一定的参考价值。

# Example Dockerfile
FROM hello-world
{
  "AWSEBDockerrunVersion": "1",
  "Image": {
    "Name": "<AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/<NAME>:<TAG>",
    "Update": "true"
  },
  "Ports": [
    {
      "ContainerPort": "443"
    }
  ]
}
#!/bin/bash

# usage: ./deploy.sh staging f0478bd7c2f584b41a49405c91a439ce9d944657

BRANCH=$1
SHA1=$2

AWS_ACCOUNT_ID=12345678900
NAME=name-of-service-to-deploy
EB_BUCKET=aws-s3-bucket-to-hold-application-versions

VERSION=$BRANCH-$SHA1
ZIP=$VERSION.zip

aws configure set default.region us-east-1

# Authenticate against our Docker registry
eval $(aws ecr get-login)

# Build and push the image
docker build -t $NAME:$VERSION .
docker tag $NAME:$VERSION $AWS_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/$NAME:$VERSION
docker push $AWS_ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/$NAME:$VERSION

# Replace the <AWS_ACCOUNT_ID> with the real ID
sed -i='' "s/<AWS_ACCOUNT_ID>/$AWS_ACCOUNT_ID/" Dockerrun.aws.json
# Replace the <NAME> with the real name
sed -i='' "s/<NAME>/$NAME" Dockerrun.aws.json
# Replace the <TAG> with the real version number
sed -i='' "s/<TAG>/$VERSION/" Dockerrun.aws.json

# Zip up the Dockerrun file (feel free to zip up an .ebextensions directory with it)
zip -r $ZIP Dockerrun.aws.json

aws s3 cp $ZIP s3://$EB_BUCKET/$ZIP

# Create a new application version with the zipped up Dockerrun file
aws elasticbeanstalk create-application-version --application-name $NAME-application \
    --version-label $VERSION --source-bundle S3Bucket=$EB_BUCKET,S3Key=$ZIP

# Update the environment to use the new application version
aws elasticbeanstalk update-environment --environment-name $NAME \
      --version-label $VERSION

以上是关于sh 构建Docker镜像,将其推送到AWS EC2 Container Registry,然后将其部署到AWS Elastic Beanstalk的主要内容,如果未能解决你的问题,请参考以下文章

Docker 推送到 AWS ECR 问题

如何在私有 docker 镜像中运行脚本

Gitlab CI - Docker 推送到 AWS ECR

在 AWS ECS EC2 集群中运行 docker 镜像

如何将文件添加到 EC2 实例下的容器中?

Jenkins-05-Pipeline使用 Docker构建项目并构建进行推送到私有仓库 harbor