

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

# 建立在 Amazon ECS 上使用的容器映像
<a name="create-container-image"></a>

Amazon ECS 使用任務定義中的 Docker 映像來啟動容器。Docker 是一種技術，可為您提供建置、執行、測試和部署分散式應用程式所需的工具。

Amazon ECS 會將容器化應用程式排程在 到容器執行個體，或在 到 AWS Fargate。容器化應用程式會被封裝為容器映像。此範例說明如何建立 Web 伺服器的容器映像。

您可以建立您的第一個 Docker 映像檔，並將該映像推送至 Amazon ECR (即容器登錄檔)，以便在 Amazon ECS 任務定義中使用。此引導過程假定您對 Docker 的含義和其運作方式有基本的了解。如需有關 Docker 的詳細資訊，請參閱[什麼是 Docker？](https://aws.amazon.com/docker/)與 [Docker documentation](https://docs.docker.com/get-started/docker-overview/)。

## 先決條件
<a name="create-container-image-prerequisites"></a>

開始之前，請務必先達成以下先決條件。
+ 確保您已完成 Amazon ECR 設定步驟。如需詳細資訊，請參閱 *Amazon Elastic Container Registry User Guide* 中的 [Moving an image through its lifecycle in Amazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/getting-started-cli.html)。
+ 您的使用者已具備存取和使用 Amazon ECR 服務所需的 IAM 許可。如需詳細資訊，請參閱 [Amazon ECR 受管政策](https://docs.aws.amazon.com/AmazonECR/latest/userguide/security-iam-awsmanpol.html)。
+ 您已安裝 Docker。如需 Amazon Linux 2023 的 Docker 安裝步驟，請參閱[在 AL2023 上安裝 Docker](#create-container-image-install-docker)。如需其他作業系統的相關資訊，請參閱 [Docker Desktop 概觀](https://docs.docker.com/desktop/)中的 Docker 文件。
+ 您已 AWS CLI 安裝並設定 。如需詳細資訊，請參閱 *AWS Command Line Interface User Guide* 中的 [Installing or updating to the latest version of the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。

若您沒有或不需要本機開發環境，而且您偏好透過 Amazon EC2 執行個體使用 Docker，我們提供下列步驟來使用 Amazon Linux 2023 啟動 Amazon EC2 執行個體並安裝 Docker Engine 與 Docker CLI。

### 在 AL2023 上安裝 Docker
<a name="create-container-image-install-docker"></a>

Docker 可在多個不同的作業系統上使用，包括大部分的現代 Linux 發行版本，例如 Ubuntu，甚至是 macOS 和 Windows。如需如何在特定作業系統上安裝 Docker 的詳細資訊，請前往「[Docker 安裝指南](https://docs.docker.com/engine/installation/#installation)」。

您不需要本機開發系統，就能使用 Docker。如果您已在使用 Amazon EC2，則可以啟動 Amazon Linux 2023 執行個體，並安裝 Docker 以開始使用。

如果您已經安裝 Docker，請跳到「[建立 Docker 映像](#create-container-image-create-image)」。

**使用 Amazon Linux 2023 AMI 在 Amazon EC2 執行個體上 安裝 Docker**

1. 使用最新版 Amazon Linux 2023 AMI 啟動執行個體。如需詳細資訊，請參閱 *Amazon EC2 User Guide* 中的 [Launch an EC2 instance using the launch instance wizard in the console](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-instance-wizard.html)。

1. 連線到您的執行個體。如需詳細資訊，請參閱 *Amazon EC2 User Guide* 中的 [Connect to your EC2 instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect.html)。

1. 更新已安裝的套裝服務，並在執行個體上封裝快取。

   ```
   sudo yum update -y
   ```

1. 安裝最新的 Docker Community Edition 套裝服務。

   ```
   sudo yum install docker
   ```

1. 啟動 Docker 服務。

   ```
   sudo service docker start
   ```

1. 將 `ec2-user` 新增至 `docker` 群組，讓您可以在不使用 `sudo` 的情況下執行 Docker 命令。

   ```
   sudo usermod -a -G docker ec2-user
   ```

1. 登出並重新登入，以取得新的 `docker` 群組許可。關閉目前的 SSH 終端機視窗，即可完成此操作，並在新的 SSH 終端機視窗中重新連接至執行個體。新的 SSH 工作階段將會有適當的 `docker` 群組許可。

1. 驗證 `ec2-user` 可以在不使用 `sudo` 的情況下執行 Docker 命令。

   ```
   docker info
   ```
**注意**  
在某些情況下，您可能需要重新啟動執行個體，才能提供 `ec2-user` 存取 Docker 常駐程式的許可。如果您看到下列錯誤，請嘗試重新啟動執行個體：  

   ```
   Cannot connect to the Docker daemon. Is the docker daemon running on this host?
   ```

## 建立 Docker 映像
<a name="create-container-image-create-image"></a>

Amazon ECS 任務定義使用容器映像啟動您叢集中容器執行個體上的容器。在本節中，您將建立簡單 Web 應用程式的 Docker 映像，並在本機系統或 Amazon EC2 執行個體上進行測試，然後將映像推送至 Amazon ECR 容器登錄檔，讓您可以在 Amazon ECS 任務定義中使用。

**建立簡單 Web 應用程式的 Docker 映像**

1. 建立稱為 `Dockerfile` 的檔案。Dockerfile 是一種資訊清單，說明用於您 Docker 映像的基本映像，以及您要安裝並在其上執行的項目。如需 Dockerfile 的詳細資訊，請前往「[Dockerfile 參考](https://docs.docker.com/reference/dockerfile/)」。

   ```
   touch Dockerfile
   ```

1. 編輯您剛建立的 `Dockerfile`，並新增下列內容。

   ```
   FROM public.ecr.aws/amazonlinux/amazonlinux:latest
   
   # Update installed packages and install Apache
   RUN yum update -y && \
    yum install -y httpd
   
   # Write hello world message
   RUN echo 'Hello World!' > /var/www/html/index.html
   
   # Configure Apache
   RUN echo 'mkdir -p /var/run/httpd' >> /root/run_apache.sh && \
    echo 'mkdir -p /var/lock/httpd' >> /root/run_apache.sh && \
    echo '/usr/sbin/httpd -D FOREGROUND' >> /root/run_apache.sh && \
    chmod 755 /root/run_apache.sh
   
   EXPOSE 80
   
   CMD /root/run_apache.sh
   ```

   此 Dockerfile 使用 Amazon ECR Public 上託管的公有 Amazon Linux 2023 映像。`RUN` 指令會更新套件快取，並安裝 Web 伺服器的一些軟體套件服務，然後寫入 "Hello World\$1" 內容至 Web 伺服器文件根目錄。`EXPOSE` 指令指容器上的連接埠 80 為接聽連接埠，而 `CMD` 指令會啟動 Web 伺服器。

1. <a name="sample-docker-build-step"></a>從 Dockerfile 建置 Docker 映像。
**注意**  
在下列命令中，有些 Docker 版本可能需要 Dockerfile 的完整路徑，而不是下面所示的相對路徑。  
如果您在基於 ARM 的系統中執行命令，例如 [Apple Silicon](https://support.apple.com/en-gb/116943)，請使用 --platform option "--platform linux/amd64"。

   ```
   docker build -t hello-world .
   ```

1. 列出您的容器映像。

   ```
   docker images --filter reference=hello-world
   ```

   輸出：

   ```
   REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
   hello-world         latest              e9ffedc8c286        4 minutes ago       194MB
   ```

1. 執行新建置的映像。`-p 80:80` 選項會將容器上的公開連接埠 80 映射至主機系統上的連接埠 80。

   ```
   docker run -t -i -p 80:80 hello-world
   ```
**注意**  
Apache Web 伺服器中的輸出會顯示在終端機視窗中。您可以忽略 "`Could not reliably determine the fully qualified domain name`" 訊息。

1. 開啟瀏覽器，然後指向執行 Docker 並託管容器的伺服器。
   + 如果您使用的是 EC2 執行個體，則這是伺服器的「公有 DNS」****值，這是您使用 SSH 來連線至執行個體的同個地址。請確定您執行個體的安全群組允許連接埠 80 上的入站流量。
   + 如果您在本機執行 Docker，請將瀏覽器指向 [http://localhost/](http://localhost/)。

   您應該會看到網頁，內含您的 "Hello World\$1" 陳述式。

1. 輸入 **Ctrl \$1 c**，以停止 Docker 容器。

## 推送映像至 Amazon Elastic Container Registry
<a name="create-container-image-push-ecr"></a>

Amazon ECR 是受管的 AWS 受管映像登錄服務。您可以使用 Docker CLI 在 Amazon ECR 儲存庫中推送、提取與管理映像。如需 Amazon ECR 產品詳細資訊、特色客戶案例研究和常見問答集，請參閱 [Amazon Elastic Container Registry 產品詳細資訊頁面](https://aws.amazon.com/ecr)。

**標記映像並將之推送至 Amazon ECR**

1. 建立 Amazon ECR 儲存庫，以便存放 `hello-world` 映像。請記下輸出中的 `repositoryUri`。

   `region`以 取代 AWS 區域，例如 `us-east-1`。

   ```
   aws ecr create-repository --repository-name hello-repository --region region
   ```

   輸出：

   ```
   {
       "repository": {
           "registryId": "aws_account_id",
           "repositoryName": "hello-repository",
           "repositoryArn": "arn:aws:ecr:region:aws_account_id:repository/hello-repository",
           "createdAt": 1505337806.0,
           "repositoryUri": "aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository"
       }
   }
   ```

1. 為 `hello-world` 映像標記上一步中的 `repositoryUri` 值。

   ```
   docker tag hello-world aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository
   ```

1. 執行 **aws ecr get-login-password** 命令。指定您要驗證的登錄 URI。如需詳細資訊，請參閱《Amazon Elastic Container Registry 使用者指南》**中的[登錄檔身分驗證](https://docs.aws.amazon.com/AmazonECR/latest/userguide/Registries.html#registry_auth)。

   ```
   aws ecr get-login-password --region region | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com
   ```

   輸出：

   ```
   Login Succeeded
   ```
**重要**  
若您收到錯誤，請安裝或升級至最新版本的 AWS CLI。如需詳細資訊，請參閱 *AWS Command Line Interface User Guide* 中的 [Installing or updating to the latest version of the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。

1. 使用先前步驟中的 `repositoryUri` 值，將映像推送至 Amazon ECR。

   ```
   docker push aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository
   ```

## 清除
<a name="create-container-image-cleanup"></a>

若要繼續建立 Amazon ECS 任務定義並使用容器映像啟動任務，請跳到 [後續步驟](#create-container-image-next-steps)。在您試驗完 Amazon ECR 映像後，即可刪除儲存庫，這樣就不會向您收取映像儲存的費用。

```
aws ecr delete-repository --repository-name hello-repository --region region --force
```

## 後續步驟
<a name="create-container-image-next-steps"></a>

任務定義需要任務執行角色。如需詳細資訊，請參閱[Amazon ECS 任務執行 IAM 角色](task_execution_IAM_role.md)。

建立容器映像並將其推送至 Amazon ECR 之後，您便可以在任務定義中使用該映像。如需詳細資訊，請參閱下列其中一個項目：
+ [了解如何為 Fargate 建立 Amazon ECS Linux 任務](getting-started-fargate.md)
+ [了解如何為 Fargate 建立 Amazon ECS Windows 任務](Windows_fargate-getting_started.md)
+ [使用 為 Fargate 建立 Amazon ECS Linux 任務 AWS CLI](ECS_AWSCLI_Fargate.md)