容器儲存體概觀Container Storage Overview

本主題提供容器在 Windows 上使用儲存空間的各種方式概觀。This topic provides an overview of the different ways containers use storage on Windows. 容器在處理儲存體方面的行為與虛擬機器不同。Containers behave differently than virtual machines when it comes to storage. 本質上,建置容器是為了防止在其內部執行的應用程式在主機的檔案系統上寫入狀態。By nature, containers are built to prevent an app running within them from writing state all over the host's filesystem. 容器預設會使用「臨時」空間,但 Windows 也提供保存儲存體的方法。Containers use a "scratch" space by default, but Windows also provides a means to persist storage.

臨時空間Scratch Space

Windows 容器預設會使用暫時儲存體。Windows containers by default use ephemeral storage. 所有容器 I/O 都會在「臨時空間」進行,且每個容器都會取得其自己的臨時空間。All container I/O happens in a "scratch space" and each container gets their own scratch. 檔案建立和檔案寫入會在臨時空間進行擷取,而不會逸出到主機。File creation and file writes are captured in the scratch space and do not escape to the host. 如果容器執行個體已停止,則會擲回在臨時空間發生的所有變更。When a container instance is stopped, all changes that occurred in the scratch space are thrown away. 啟動新的容器執行個體後,就會為該執行個體提供新的臨時空間。When a new container instance is started, a new scratch space is provided for the instance.

分層儲存體Layer Storage

容器概觀所述,容器映像是以一系列圖層表示的檔案組合。As described in the Containers Overview, container images are a bundle of files expressed as a series of layers. 分層儲存體是所有內建於容器中的檔案。Layer storage is all the files that are built into the container. 每次 docker pull 然後 docker run 該容器,兩者相同。Every time you docker pull then docker run that container - they are the same.

分層儲存的位置,以及如何變更Where layers are stored and how to change it

在預設安裝中,分層儲存在 C:\ProgramData\docker 中並分散至 "image" 和 "windowsfilter" 目錄中。In a default installation, layers are stored in C:\ProgramData\docker and split across the "image" and "windowsfilter" directories. 您可以使用 docker-root 組態來變更分層儲存的位置,如 Windows 上的 Docker 引擎文件所示範。You can change where the layers are stored using the docker-root configuration, as demonstrated in the Docker Engine on Windows documentation.

注意

分層儲存體僅支援 NTFS,Only NTFS is supported for layer storage. 不支援 ReFS。ReFS is not supported.

您不應該修改分層目錄中的任何檔案 - 使用以下命令謹慎管理檔案:You should not modify any files in the layer directories - they're carefully managed using commands such as:

分層儲存體支援的作業Supported operations in layer storage

執行中的容器可使用除交易外的大部分 NTFS 作業,Running containers can use most NTFS operations with the exception of transactions. 包括設定 ACL,而且所有 ACL 都是在容器內檢查。This includes setting ACLs, and all ACLs are checked inside the container. 如果您想要在容器中以多個使用者執行處理序,您可以在 Dockerfile 中使用 RUN net user /create ... 建立使用者,設定檔案 ACL,然後使用 Dockerfile USER 指示詞,設定處理序以該使用者執行。If you want to run processes as multiple users inside a container, you can create users in your Dockerfile with RUN net user /create ..., set file ACLs, then configure processes to run with that user using the Dockerfile USER directive.

永續性儲存體Persistent Storage

Windows 容器支援透過繫結裝載和磁碟區來提供永續性儲存體的機制。Windows containers support mechanisms for providing persistent storage via bind mounts and volumes. 若要深入了解,請參閱容器中的永續性儲存體To learn more, see Persistent Storage in Containers.

儲存體限制Storage Limits

Windows 應用程式的通用模式是先查詢可用磁碟空間量,再安裝或建立新的檔案,或做為清除暫存檔案的觸發程序。A common pattern for Windows applications is to query the amount of free disk space before installing or creating new files or as a trigger for cleaning up temporary files. 以最大化應用程式相容性為目標,Windows 容器中的 C: 磁碟機代表 20GB 的虛擬可用大小。With the goal of maximizing application compatibility, the C: drive in a Windows container represents a virtual free size of 20GB.

有些使用者可能想要覆寫此預設值,並將可用空間設定為較小或較大的值。Some users may want to override this default and configure the free space to a smaller or larger value. 這可透過 “storage-opt” 設定內的 “size” 選項來完成。this can be accomplished though the “size” option within the “storage-opt” configuration.

範例Examples

命令列:docker run --storage-opt "size=50GB" mcr.microsoft.com/windows/servercore:ltsc2019 cmdCommand line: docker run --storage-opt "size=50GB" mcr.microsoft.com/windows/servercore:ltsc2019 cmd

或者,您可以直接變更 Docker 組態檔:Or you can change the docker configuration file directly:

"storage-opts": [
    "size=50GB"
  ]

提示

此方法也適用於 Docker 組建。This method works for docker build, too. 如需修改 Docker 組態檔的詳細資訊,請參閱設定 Docker 文件。See the configure docker document for more details on modifying the docker configuration file.