如何上传 Project+Dockerfile 并在 AWS 上构建而不是在本地构建并将 docker 图像上传到 ECS?

How to upload Project+Dockerfile and build on AWS rather than build locally and upload the docker image to ECS?

因为我的项目 (nodejs) + Dockerfile 很小 (<10mb) 但 docker 图像可以达到 700mb。

作为对比, 在本地构建我的 docker(使用预先下载的 docker 映像库,即 OS)并安装 node_modules 大约需要 30 秒。

将构建的 docker 图像 (700mb) 上传到 Amazon ECS 大约需要 10 分钟。

所以我在想是否可以将我的项目和 Dockerfile 上传到 AWS,运行在那里构建,我希望他们也能管理 intermediate/basic 图像。

与 700mb 相比,我希望只上传 10mb 小得多的文件,并且 运行 docker 构建 30 秒

你可以通过两种方式做到这一点

第一个是最好的方法,许多行业标准都遵循这一点,这里也提到了。

https://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-basics.html

第一名:

创建小型或微型 t2 实例。分配 role to that instance 或者您可以 configure AWS cli。然后在 EC2 实例中克隆你的项目+dockerfile

因此,与本地系统相比,您的构建过程和推送会更快。

eval $(aws ecr get-login --no-include-email --region us-west-2)
docker build -t hello-world .
docker tag hello-world aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world
docker push aws_account_id.dkr.ecr.us-east-1.amazonaws.com/hello-world

第 2 个:

在容器实例中克隆您的项目+dockerfile 并在 ECS container instance 而不是本地系统中构建您的图像,并将该图像推送到 AWS ECR。如步骤 1 中所述。您需要配置 AWS cli 或最佳方法是将角色分配给您的容器实例。

https://aws.amazon.com/blogs/security/easily-replace-or-attach-an-iam-role-to-an-existing-ec2-instance-by-using-the-ec2-console/