aws fargate
动机 (Motivation)
Docker is a fantastic tool to encapsulate and deploy applications in an easy and scalable way. Indeed, something I find myself doing very often is wrapping Python libraries into Docker images that I can later use as boilerplates for my projects.
Docker是一种出色的工具,可以轻松,可扩展地封装和部署应用程序。 确实,我发现自己经常做的事情是将Python库包装到Docker映像中,以后可以用作我的项目的样板。
In this post, I will illustrate how to register your Docker images in a container registry and how to deploy the containers in AWS using Fargate, a serverless compute engine designed to run containerized applications. Additionally, we will use Cloud Formation to deploy our stack in a programmatic way. Let’s get started!
在本文中,我将说明如何在容器注册表中注册Docker映像,以及如何使用Fargate(一种旨在运行容器化应用程序的无服务器计算引擎)在AWS中部署容器。 此外,我们将使用Cloud Formation以编程方式部署堆栈。 让我们开始吧!
在容器注册表中注册图像 (Register your images in a container registry)
Container registries are to Docker images what code repositories are to code. In a registry, you create image repositories to push and register your local images, you can store different versions of the same image, and other users can pull and update the image if they have access to the repo.
容器注册表对于Docker映像来说是代码存储库所要编码的代码。 在注册表中,您可以创建映像存储库来推送和注册本地映像,可以存储同一映像的不同版本,并且其他用户(如果他们可以访问该存储库)可以提取和更新该映像。
Now, let’s say you have developed locally an image that you want to deploy to the cloud. To do so, we would need to store our local image in a container registry from which it can be pulled and deployed. In this article, I will be using a fairly simple image that starts a web Python-Dash application on port 80.
现在,假设您已经在本地开发了要部署到云的映像。 为此,我们需要将本地映像存储在容器注册表中,以便从中进行部署和部署。 在本文中,我将使用一个相当简单的图像在端口80上启动Web Python-Dash应用程序。
We will use the ECR (Elastic Container Registry) to register our images. ECR is an AWS service, quite similar to DockerHub, to store Docker images. The first thing we have to do is creating a repository in ECR, we can use the AWS CLI as follows:
我们将使用ECR (弹性容器注册表)注册我们的图像。 ECR是一项与DockerHub非常相似的AWS服务,用于存储Docker映像。 我们要做的第一件事是在ECR中创建存储库,我们可以如下使用AWS CLI:
aws ecr create-repository \
--repository-name \
--image-scanning-configuration scanOnPush=
--region eu
You should be able to see the repository in the AWS management console
您应该能够在AWS管理控制台中查看存储库
To push local images to our ECR repository we are required to authenticate our local Docker CLI into AWS:
要将本地映像推送到我们的ECR存储库,我们需要将本地Docker CLI验证到AWS:
aws ecr get-login-password --region region | docker login --username AWS --password-stdin acccount_id.dkr.ecr.region.amazonaws.com
Just replace the aws_account_id
and region
appropriately. You should see the message Login Succeeded
in the terminal, which means our local Docker CLI is authenticated to interact with the ECR.
只需适当地替换aws_account_id
和region
。 您应该在终端上看到“ Login Succeeded
”消息,这意味着我们的本地Docker CLI已通过身份验证,可以与ECR交互。
Let’s push now our local image to our brand new repository. To do so we must tag our image to point to the ECR repository:
现在让我们将本地图像推送到全新的存储库中。 为此,我们必须标记图像以指向ECR存储库:
docker tag image_id account_id.dkr.ecr.eu-central-1.amazonaws.com/dash-app:latest
Now we just have to push it to the ECR:
现在我们只需要将其推送到ECR:
docker push account_id.dkr.ecr.eu-central-1.amazonaws.com/dash-app:latest
You should see the pushed image in the AWS Console:
您应该在AWS控制台中看到推送的图像: