1. 程式人生 > 實用技巧 >【Docker】Dockerfile 之 VOLUME

【Docker】Dockerfile 之 VOLUME

參考教程:https://docs.docker.com/engine/reference/builder/

環境

  1. virtual box 6.1
  2. centos 7.8
  3. docker 19.03

資料卷

VOLUME ["/data"]

The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers. The value can be a JSON array, VOLUME ["/var/log/"]

, or a plain string with multiple arguments, such as VOLUME /var/log or VOLUME /var/log /var/db. For more information/examples and mounting instructions via the Docker client, refer to Share Directories via Volumes documentation.

VOLUME 指令建立具有指定名稱的掛載點,並將其標記為儲存來自本地主機或其他容器的外部安裝的資料卷。該值可以是 JSON 陣列,VOLUME ["/var/log/"]

,也可以是帶有多個引數的純字串,例如 VOLUME /var/logVOLUME /var/log /var/db 。有關通過 Docker 客戶端的更多掛載說明,請參考 Share Directories via Volumes 文件。

The docker run command initializes the newly created volume with any data that exists at the specified location within the base image. For example, consider the following Dockerfile snippet:

docker run 命令使用基礎映象內指定位置上存在的任何資料初始化新建立的卷。例如,考慮以下 Dockerfile 片段:

FROM ubuntu
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting
VOLUME /myvol

This Dockerfile results in an image that causes docker run to create a new mount point at /myvol and copy the greeting file into the newly created volume.

這個 Dockerfile 生成一個映象,該映象使 docker run/myvol 目錄建立一個新的掛載點,並將 greeting 檔案複製到新建立的卷中。

關於指定卷的注意事項

Keep the following things in mind about volumes in the Dockerfile.
關於 Dockerfile 中的卷,請記住以下幾點。

  • Volumes on Windows-based containers: When using Windows-based containers, the destination of a volume inside the container must be one of:

  • 基於 Windows 的容器上的卷:使用基於 Windows 的容器時,容器內卷的目的地必須是以下之一:

    • a non-existing or empty directory
    • a drive other than C:
    • 不存在或空目錄
    • 除C:以外的驅動器
  • Changing the volume from within the Dockerfile: If any build steps change the data within the volume after it has been declared, those changes will be discarded.

  • 從 Dockerfile 內更改卷:如果在聲明瞭卷後有任何構建步驟更改了卷中的資料,則這些更改將被丟棄。

  • JSON formatting: The list is parsed as a JSON array. You must enclose words with double quotes (") rather than single quotes (').

  • JSON 格式:該列表被解析為 JSON 陣列。您必須用雙引號(")而不是單引號(')括起單詞。

  • The host directory is declared at container run-time: The host directory (the mountpoint) is, by its nature, host-dependent. This is to preserve image portability, since a given host directory can’t be guaranteed to be available on all hosts. For this reason, you can’t mount a host directory from within the Dockerfile. The VOLUME instruction does not support specifying a host-dir parameter. You must specify the mountpoint when you create or run the container.

  • 主機目錄是在容器執行時宣告的:主機目錄(掛載點)從本質上說是依賴於主機的。這是為了保留映象的可移植性,因為不能保證給定的主機目錄在所有主機上都可用。因此,您無法從 Dockerfile 中掛載主機目錄。VOLUME 指令不支援指定 host-dir 引數。建立或執行容器時,必須指定掛載點。

總結

介紹了 Dockerfile 中 VOLUME 指令的用法和注意事項。