在 Amazon Lightsail 上部署容器 Web 应用程序
入门指南
模块 3:部署容器
在本模块中,我们将我们的容器部署到云中
简介
现在,您的 Amazon Lightsail 容器服务已准备就绪,下一步是部署容器服务。
Amazon Lightsail 能够从公共容器映像存储库(例如 Docker Hub、Amazon ECR Public Gallery 或您的本地机器)部署容器。
在本指南中,我们将直接从您的本地计算机进行部署。
您将学到的内容
- 如何将本地容器映像推送到 Amazon Lightsail
- 如何在容器服务上部署容器映像版本
完成所需时间
10 分钟
模块先决条件
- 具有管理员级访问权限的 AWS 账户**
- 推荐的浏览器:最新版 Chrome 或 Firefox
[**] 过去 24 小时内创建的账户可能尚不具有访问此教程所需服务的权限。
实施
推送本地容器映像
部署容器的第一步是将容器映像推送到 Amazon Lightsail
在您创建容器的目录中打开一个终端并键入以下命令。确保使用您从中创建 Lightsail 容器服务的同一区域。
aws lightsail push-container-image \
--region eu-west-3 \
--service-name signup-application \
--label latest \
--image demo-flask-signup:latest
# the command outputs the following lines
f017a6ddb209: Pushed
b94dee417b5e: Pushed
37d77b23a488: Pushed
8e77a3b871e7: Pushed
4bc5d67b6427: Pushed
ce8ffb5c560e: Pushed
4226d5d0360f: Pushed
9d1af766c818: Pushed
d97733c0a3b6: Pushed
c553c6ba5f13: Pushed
48b4a40de359: Pushed
ace9ed9bcfaf: Pushed
764055ebc9a7: Pushed
Digest: sha256:128f84907d30a1fb47c1888720485fa8050cc99bc1034e0cfd1f46d3b6e57e19
Image "demo-flask-signup:latest" registered.
Refer to this image as ":signup-application.latest.1" in deployments.
该命令调用 docker push 将您的图像上传到 Amazon Lightsail。这可能需要几分钟才能完成,具体取决于您的网络带宽。
输出的最后一行给出容器的内部名称,在我们的例子中是 :signup-application.latest.1。记下它,因为我们需要使用该名称来将容器部署到容器服务上。如果您想在稍后阶段访问容器名称,可以键入以下命令:
aws lightsail get-container-images \
--region eu-west-3 \
--service-name signup-application
# the command outputs the following lines
{
"containerImages": [
{
"image": ":signup-application.latest.1",
"digest": "sha256:128f84907d30a1fb47c1888720485fa8050cc99bc1034e0cfd1f46d3b6e57e19",
"createdAt": "2021-07-17T15:11:49+02:00"
}
]
}
将本地容器映像推送到 Lightsail
现在,容器映像存储在 Amazon Lightsail 上,我们可以将该映像部署到容器服务。
Lightsail 需要以下信息来创建部署:
- 要部署的容器映像(如上一个命令返回的 :signup-application.latest.1)容器的名称
- 容器公开的网络端口(如 Dockerfile 配置中所述)
- (可选)可公开访问的网络端点的详细信息:TCP 端口号和协议,IP 将自动分配。
所有这些信息都存储在您需要在部署之前创建的 JSON 文件中。
创建一个名为 lc.json 的 JSON 文件。如果您正在使用下载的代码,则需求修改与容器位于同一目录中的现有 lc.json 文件。将以下 json 文件复制到文件中:
{
"serviceName": "signup-application",
"containers": {
"signup-app-container": {
"image": ":signup-application.latest.1",
"ports": {
"80": "HTTP"
}
}
},
"publicEndpoint": {
"containerName": "signup-app-container",
"containerPort": 80
}
}
然后,通过键入以下命令来部署容器:
aws lightsail create-container-service-deployment \
--region eu-west-3 \
--cli-input-json file://lc.json
# the command outputs the following
{
"containerService": {
"containerServiceName": "signup-application",
"arn": "arn:aws:lightsail:eu-west-3:012345678901:ContainerService/528a0bcf-fd14-42d4-a09a-943a241adc51",
"createdAt": "2021-07-17T13:38:40+02:00",
"location": {
"availabilityZone": "all",
"regionName": "eu-west-3"
},
"resourceType": "ContainerService",
"tags": [],
"power": "nano",
"powerId": "nano-1",
"state": "DEPLOYING",
"scale": 2,
"nextDeployment": {
"version": 1,
"state": "ACTIVATING",
"containers": {
"signup-app-container": {
"image": ":signup-application.latest.1",
"command": [],
"environment": {},
"ports": {
"80": "HTTP"
}
}
},
"publicEndpoint": {
"containerName": "signup-app-container",
"containerPort": 80,
"healthCheck": {
"healthyThreshold": 2,
"unhealthyThreshold": 2,
"timeoutSeconds": 2,
"intervalSeconds": 5,
"path": "/",
"successCodes": "200-499"
}
},
"createdAt": "2021-07-17T15:31:01+02:00"
},
"isDisabled": false,
"principalArn": "arn:aws:iam::577312533299:role/amazon/lightsail/eu-west-3/containers/signup-application/1jetjd21p8qshe57seuh71tnrnn29ps77lnno20sbgl2ghbuc0r0",
"privateDomainName": "signup-application.service.local",
"url": "https://signup-application.me04fvc6dbk4e.eu-west-3.cs.amazonlightsail.com/"
}
}
请注意 "state": "DEPLOYING" 状态。一段时间后,状态将变为 ACTIVE。完成部署大约需要几分钟。您可以使用以下命令检查部署的状态:
# while deploying
aws lightsail get-container-services \
--region eu-west-3 \
--query "containerServices[].nextDeployment.state"
# once deployed
aws lightsail get-container-services \
--region eu-west-3 \
--query "containerServices[].currentDeployment.state"
测试部署
要测试部署,请先检索为您创建的 URL Lightsail。打开一个终端,然后键入以下命令:
aws lightsail get-container-services \
--region eu-west-3 \
--query "containerServices[].url"
# the command outputs something like the below
[
"https://signup-application.me04fvc6dbk4e.eu-west-3.cs.amazonlightsail.com/"
]
打开您喜欢的浏览器并将其指向您的部署 URL。

或者,您可以使用 cURL 从命令行测试端点:
curl -I https://signup-application.me04fvc6dbk4e.eu-west-3.cs.amazonlightsail.com/
HTTP/2 200
date: Sat, 17 Jul 2021 13:38:04 GMT
content-type: text/html; charset=utf-8
content-length: 4579
server: nginx/1.21.1
恭喜,您的容器现已部署到 Amazon Lightsail 上。