

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

# Amazon ECS 的 Docker 磁碟區範例
<a name="docker-volume-examples"></a>

下列範例示範如何為容器提供暫時性儲存，以及如何為多個容器提供共用磁碟區，以及如何為容器提供 NFS 持久性儲存。

**使用 Docker 磁碟區為單個容器提供暫時性儲存**

在此範例中，容器使用任務完成後清除的空白資料磁碟區。舉一個使用案例範例，您的容器在任務期間需要存取一些暫存檔案儲存位置。使用 Docker 磁碟區可達成此任務。

1. 在任務定義 `volumes` 區段，以 `name` 和 `DockerVolumeConfiguration` 值定義資料磁碟區。在本範例中，我們指定範圍為 `task`，所以會在任務停止後刪除磁碟區，並使用內建的 `local` 驅動程式。

   ```
   "volumes": [
       {
           "name": "scratch",
           "dockerVolumeConfiguration" : {
               "scope": "task",
               "driver": "local",
               "labels": {
                   "scratch": "space"
               }
           }
       }
   ]
   ```

1. 在 `containerDefinitions` 區段中定義容器，並使用 `mountPoints` 值來參考已定義的磁碟區名稱，使用 `containerPath` 值將磁碟區掛載於容器。

   ```
   "containerDefinitions": [
       {
           "name": "container-1",
           "mountPoints": [
               {
                 "sourceVolume": "scratch",
                 "containerPath": "/var/scratch"
               }
           ]
       }
   ]
   ```

**使用 Docker 磁碟區為多個容器提供持續性儲存**

在此範例中，您要多個容器使用一個共用磁碟區，並且要在使用它的任何單一任務停止之後加以保留。正在使用內建的 `local` 驅動程式。如此一來，磁碟區仍受限於容器執行個體的生命週期。

1. 在任務定義 `volumes` 區段，以 `name` 和 `DockerVolumeConfiguration` 值定義資料磁碟區。在此範例中，指定 `shared` 範圍，以便持續保留磁碟區，請將 autoprovision 設定為 `true`。如此一來，已建立該磁碟區以供使用。然後，還可以使用內建 `local` 驅動程式。

   ```
   "volumes": [
       {
           "name": "database",
           "dockerVolumeConfiguration" : {
               "scope": "shared",
               "autoprovision": true,
               "driver": "local",
               "labels": {
                   "database": "database_name"
               }
           }
       }
   ]
   ```

1. 在 `containerDefinitions` 區段中定義容器，並使用 `mountPoints` 值來參考已定義的磁碟區名稱，使用 `containerPath` 值將磁碟區掛載於容器。

   ```
   "containerDefinitions": [
       {
           "name": "container-1",
           "mountPoints": [
           {
             "sourceVolume": "database",
             "containerPath": "/var/database"
           }
         ]
       },
       {
         "name": "container-2",
         "mountPoints": [
           {
             "sourceVolume": "database",
             "containerPath": "/var/database"
           }
         ]
       }
     ]
   ```

**使用 Docker 磁碟區提供容器的 NFS 持久性儲存**

 在此範例中，容器使用 NFS 資料磁碟區，該資料磁碟區在任務啟動時自動掛載並在任務停止時自動卸載。這可使用 Docker 內建 `local` 驅動程式加以實現。一個範例使用案例是，您可能有本機 NFS 儲存體，並且需要從 ECS Anywhere 任務進行存取。這可以使用具有 NFS 驅動程式選項的 Docker 磁碟區來達成。

1. 在任務定義 `volumes` 區段，以 `name` 和 `DockerVolumeConfiguration` 值定義資料磁碟區。在此範例中，指定 `task` 範圍，以便在任務停止後卸載磁碟區。使用 `local` 驅動程式並相應地使用 `type`、`device` 和 `o` 選項設定 `driverOpts`。使用 NFS 伺服器端點取代 `NFS_SERVER`。

   ```
   "volumes": [
          {
              "name": "NFS",
              "dockerVolumeConfiguration" : {
                  "scope": "task",
                  "driver": "local",
                  "driverOpts": {
                      "type": "nfs",
                      "device": "$NFS_SERVER:/mnt/nfs",
                      "o": "addr=$NFS_SERVER"
                  }
              }
          }
      ]
   ```

1. 在 `containerDefinitions` 區段中定義容器，並使用 `mountPoints` 值來參考已定義的磁碟區名稱，使用 `containerPath` 值將磁碟區掛載在容器上。

   ```
   "containerDefinitions": [
          {
              "name": "container-1",
              "mountPoints": [
                  {
                    "sourceVolume": "NFS",
                    "containerPath": "/var/nfsmount"
                  }
              ]
          }
      ]
   ```