

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在做為 Docker 容器 AWS IoT Greengrass V2 執行時部署容器化應用程式
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container"></a>

*Salih Bakir、Giuseppe Di Bella 和 Gustav Svalander，Amazon Web Services*

## 總結
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-summary"></a>

AWS IoT Greengrass Version 2作為 Docker 容器部署時， 原生不支援執行 Docker 應用程式容器。此模式示範如何根據 AWS IoT Greengrass V2 啟用 Docker-in-Docker (DinD) 功能的最新版本 建立自訂容器映像。使用 DinD，您可以在 AWS IoT Greengrass V2 環境中執行容器化應用程式。

您可以將此模式部署為獨立的解決方案，或將其與 Amazon ECS Anywhere 等容器協同運作平台整合。在任一部署模型中，您維護包括 AWS IoT SiteWise Edge 處理功能的完整 AWS IoT Greengrass V2 功能，同時啟用可擴展的容器型部署。

## 先決條件和限制
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶。
+ 如需一般 AWS IoT Greengrass Version 2 先決條件，請參閱 AWS IoT Greengrass Version 2 文件中的[先決條件](https://docs.aws.amazon.com/greengrass/v2/developerguide/getting-started-prerequisites.html)。
+ 在 Linux、macOS 或 Windows 上安裝和設定 Docker Engine。
+ Docker Compose （如果您使用 Docker Compose 命令列界面 (CLI) 來執行 Docker 映像）。
+ Linux 作業系統。
+ 具有支援虛擬化之主機伺服器的 Hypervisor。
+ 系統需求：
  + 2 GB RAM （最低）
  + 5 GB 的可用磁碟空間 （最低）
  + 對於 AWS IoT SiteWise Edge，具有 16 GB RAM 和 50 GB 可用磁碟空間的 x86\$164 四核心 CPU。如需 AWS IoT SiteWise 資料處理的詳細資訊，請參閱 AWS IoT SiteWise 文件中的[資料處理套件需求](https://docs.aws.amazon.com/iot-sitewise/latest/userguide/configure-gateway-ggv2.html#w2aac17c19c13b7)。

**產品版本**
+ AWS IoT Greengrass Version 2 2.5.3 版或更新版本
+ Docker-in-Docker 1.0.0 版或更新版本
+ Docker Compose 1.22 版或更新版本
+ Docker 引擎 20.10.12 版或更新版本

**限制**
+ 有些 AWS 服務 完全無法使用 AWS 區域。如需區域可用性，請參閱[AWS 依區域的服務](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。如需特定端點，請參閱[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然後選擇服務的連結。

## Architecture
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-architecture"></a>

**目標技術堆疊**
+ **資料來源** – 產生資料處理資料的 IoT 裝置、感應器或工業設備
+ **AWS IoT Greengrass V2** – 做為具有 D-in-D 功能的 Docker 容器執行，部署在邊緣基礎設施上
+ **容器化應用程式** – 以巢狀 Docker 容器的形式在 AWS IoT Greengrass V2 環境中執行的自訂應用程式
+ **（選用） Amazon ECS Anywhere** – 管理容器部署的 AWS IoT Greengrass V2 容器協同運作
+ **其他 AWS 服務** – AWS IoT Core AWS IoT SiteWise和其他 AWS 服務 用於資料處理和管理

**目標架構**

下圖顯示使用 Amazon ECS Anywhere 的範例目標部署架構，Amazon ECS Anywhere 是一種容器管理工具。

![\[使用 Amazon ECS Anywhere 的部署架構。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/2ecf5354-40e0-4fd9-9798-086719059784/images/5ed2652e-9604-4809-8962-b167e1991658.png)


該圖顯示以下工作流程：

**1：容器映像儲存** – Amazon ECR 會存放 AWS IoT Greengrass 容器映像和邊緣處理所需的任何自訂應用程式容器。

**2 **和** 3：容器部署** – Amazon ECS Anywhere 會將 AWS IoT Greengrass 容器映像從 Amazon ECR 部署到節點，管理容器生命週期和部署程序。

**4：元件部署** – 部署 AWS IoT Greengrass 的核心會根據其組態自動部署其相關元件。元件包括在容器化環境中的 AWS IoT SiteWise Edge 和其他必要的邊緣處理元件。

**5：資料擷取** – 完全設定後， AWS IoT Greengrass 開始從節點的各種 IoT 資料來源擷取遙測和感應器資料。

**6：資料處理和雲端整合** – 容器化 AWS IoT Greengrass 核心會使用其部署的元件 （包括工業資料的 AWS IoT SiteWise Edge) 在本機處理資料。然後，它會將處理的資料傳送至 AWS 雲端 服務，以進一步分析和儲存。

## 工具
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-tools"></a>

**AWS 服務**
+ [Amazon ECS Anywhere](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch-type-external.html) 可協助您在自己的基礎設施上部署、使用和管理 Amazon ECS 任務和服務。
+ [Amazon Elastic Compute Cloud (Amazon EC2)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html) 在 AWS 雲端中提供可擴展的運算容量。您可以視需要啟動任意數量的虛擬伺服器，，並快速進行擴展或縮減。
+ [Amazon Elastic Container Registry (Amazon ECR)](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html) 是一種受管容器映像登錄服務，安全、可擴展且可靠。
+ [AWS IoT Greengrass](https://docs.aws.amazon.com/greengrass/v2/developerguide/what-is-iot-greengrass.html) 是一種開放原始碼物聯網 (IoT) 邊緣執行期和雲端服務，可協助您在裝置上建置、部署和管理 IoT 應用程式。
+ [AWS IoT SiteWise](https://docs.aws.amazon.com/iot-sitewise/latest/userguide/what-is-sitewise.html) 可協助您大規模收集、建模、分析和視覺化工業設備的資料。

**其他工具**
+ [Docker](https://www.docker.com/) 是一組平台即服務 (PaaS) 產品，可在作業系統層級使用虛擬化在容器中交付軟體。
+ [Docker Compose](https://docs.docker.com/compose/) 是一種用於定義和執行多容器應用程式的工具。
+ [Docker Engine](https://docs.docker.com/engine/) 是一種開放原始碼容器化技術，用於建置和容器化應用程式。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [AWS IoT Greengrass v2 Docker-in-Docker](https://github.com/aws-samples/aws-iot-greengrass-docker-in-docker) 儲存庫中使用。

## 史詩
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-epics"></a>

### 建置 AWS IoT Greengrass V2 Docker-in-Docker 映像
<a name="build-the-gg2-docker-in-docker-image"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製並導覽至儲存庫。 | 若要複製儲存庫，請使用下列命令：`git clone https://github.com/aws-samples/aws-iot-greengrass-v2-docker-in-docker.git`若要導覽至 `docker`目錄，請使用下列命令：`cd aws-iot-greengrass-v2-docker-in-docker/docker` | DevOps 工程師，AWS DevOps | 
| 建置 Docker 影像。 | 若要使用預設 （最新） 版本建置 Docker 映像，請執行下列命令：`docker build -t x86_64/aws-iot-greengrass:latest .`或者，若要使用特定版本建置 Docker 映像，請執行下列命令：`docker build --build-arg GREENGRASS_RELEASE_VERSION=2.12.0 -t x86_64/aws-iot-greengrass:2.12.0 .`若要驗證組建，請執行下列命令：`docker images \| grep aws-iot-greengrass`  | AWS DevOps、DevOps 工程師、應用程式開發人員 | 
| （選用） 推送至 Amazon ECR。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | 應用程式開發人員、AWS DevOps、DevOps 工程師 | 

### 設定 AWS 登入資料
<a name="configure-aws-credentials"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 選取身分驗證方法。 | 請選擇下列其中一個選項：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | AWS 管理員 | 
| 設定身分驗證方法。 | 對於您選取的身分驗證方法，請使用下列組態指引：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | AWS 管理員 | 

### 使用 Docker Compose 執行
<a name="run-with-docker-compose"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 `docker-compose.yml`. | 使用環境變數更新 `docker-compose.yml` 檔案，如下所示：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | DevOps 工程師 | 
| 啟動並驗證容器。 | 若要從前景開始，請執行下列命令：`docker-compose up --build`或者，若要從背景開始，請執行下列命令：`docker-compose up --build -d`若要驗證狀態，請執行下列命令：`docker-compose ps`若要監控日誌，請執行下列命令：`docker-compose logs -f` | DevOps 工程師 | 

### 使用 Docker CLI 執行
<a name="run-with-docker-cli"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 使用 Docker CLI 執行容器。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | DevOps 工程師 | 
| 驗證容器。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | DevOps 工程師 | 

### 管理容器化應用程式
<a name="manage-containerized-applications"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署應用程式。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | 應用程式開發人員 | 
| 存取和測試 Docker-in-Docker。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | DevOps 工程師 | 

### （選用） 與 Amazon ECS Anywhere 整合
<a name="optional-integrate-with-ecs-anywhere"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 Amazon ECS 叢集。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | AWS 管理員 | 
| 部署 Amazon ECS 任務。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | AWS 管理員 | 

### 停止和清除
<a name="stop-and-cleanup"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 停止容器。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | DevOps 工程師 | 

## 疑難排解
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 容器無法從許可錯誤開始。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)`--privileged` 會將延伸權限授予容器。 | 
| 佈建失敗並出現登入資料錯誤。 | 若要驗證登入資料設定是否正確，請使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)確定 IAM 許可包含 `iot:CreateThing`、`iot:CreatePolicy`、`iam:CreateRole`、 `iot:AttachPolicy`和 `iam:AttachRolePolicy`。 | 
| 無法連線到容器內的 Docker 協助程式。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | 
| 容器用盡磁碟空間。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)確保磁碟空間下限：基本操作為 5 GB， AWS IoT SiteWise 邊緣為 50 GB | 
| 建置問題。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html) | 
| 網路連線問題。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)確認防火牆允許傳出 HTTPS (443) 和 MQTT (8883) 流量。 | 
| Greengrass 元件無法部署。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)檢查 `/greengrass/v2/logs/`目錄中的元件特定日誌。 | 
| 容器會在啟動後立即結束。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container.html)如果 ，請確認已正確設定所有必要的環境變數`PROVISION=true`。啟動容器時，請務必使用 `--init`旗標。 | 

## 相關資源
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-resources"></a>

**AWS resources**
+ [Amazon Elastic Container Service](https://aws.amazon.com/ecs/)
+ [設定 AWS IoT SiteWise 模型和資產的邊緣資料處理](https://docs.aws.amazon.com/iot-sitewise/latest/userguide/edge-processing.html)
+ [什麼是 AWS IoT Greengrass](https://docs.aws.amazon.com/greengrass/v2/developerguide/what-is-iot-greengrass.html)

**其他資源**
+ [Docker 文件](https://docs.docker.com/)

## 其他資訊
<a name="deploy-containerized-applications-on-aws-iot-greengrass-version-2-running-as-a-docker-container-additional"></a>
+ 對於 AWS IoT SiteWise Edge 資料處理，Docker 必須在 AWS IoT Greengrass 環境中可用。
+ 若要執行巢狀容器，您必須使用管理員層級登入資料執行 AWS IoT Greengrass 容器。