【Docker】Dockerfile 之 COPY


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

環境

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

COPY

COPY has two forms:

COPY 指令有兩種格式

COPY [--chown=<user>:<group>] <src>... <dest>
COPY [--chown=<user>:<group>] ["<src>",... "<dest>"]

This latter form is required for paths containing whitespace

包含空格的路徑需要后一種形式

The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

COPY 指令從 <src> 復制新文件或目錄,並將它們添加到容器的文件系統中,路徑為 <dest>

Multiple <src> resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build.

可以指定多個 <src> 資源,但是文件和目錄的路徑將被解釋為相對於構建上下文。

Each <src> may contain wildcards and matching will be done using Go’s filepath.Match rules. For example:

每個 <src> 可能包含通配符,並且匹配將使用 Go 的 filepath.Match 規則進行。例如:

To add all files starting with “hom”:

要添加所有以 “hom” 開頭的文件:

COPY hom* /mydir/

In the example below, ? is replaced with any single character, e.g., “home.txt”.

在下面的示例中,? 被替換為任何單個字符,例如 “home.txt”。

COPY hom?.txt /mydir/

The <dest> is an absolute path, or a path relative to WORKDIR, into which the source will be copied inside the destination container.

<dest> 是絕對路徑,或者是相對於 WORKDIR 的路徑,源將被復制到目標容器內。

The example below uses a relative path, and adds “test.txt” to <WORKDIR>/relativeDir/:

下面的示例使用相對路徑,並將 “test.txt” 添加到 <WORKDIR>/relativeDir/

COPY test.txt relativeDir/

Whereas this example uses an absolute path, and adds “test.txt” to /absoluteDir/

而此示例使用了絕對路徑,並將 “test.txt” 添加到 /absoluteDir/

COPY test.txt /absoluteDir/

When copying files or directories that contain special characters (such as [ and ]), you need to escape those paths following the Golang rules to prevent them from being treated as a matching pattern. For example, to copy a file named arr[0].txt, use the following;

在添加包含特殊字符(例如 [])的文件或目錄時,您需要按照 Golang 規則轉義那些路徑,以防止將它們視為匹配模式。例如,要添加名為 arr[0].txt 的文件,請使用以下命令;

COPY arr[[]0].txt /mydir/

Note

If you build using STDIN (docker build - < somefile), there is no build context, so COPY can’t be used.

注意

如果您使用 STDIN 進行構建(docker build - < somefile),則沒有構建上下文,因此無法使用 COPY

Optionally COPY accepts a flag --from=<name> that can be used to set the source location to a previous build stage (created with FROM .. AS <name>) that will be used instead of a build context sent by the user. In case a build stage with a specified name can’t be found an image with the same name is attempted to be used instead.

可選地,COPY 接受一個標志 --from=<name>,該標志可用於將源位置設置為先前的構建階段(由 FROM .. AS <name> 創建),該標志將代替構建用戶發送的上下文。如果找不到具有指定名稱的構建階段,則嘗試改用具有相同名稱的鏡像。

COPY obeys the following rules:

COPY 遵守以下規則:

  • The <src> path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

  • <src> 路徑必須在構建的上下文內部;您不能執行 COPY ../something /something,因為 docker build 的第一步是將上下文目錄(和子目錄)發送到 docker 守護進程。

  • If <src> is a directory, the entire contents of the directory are copied, including filesystem metadata.

  • 如果 <src> 是目錄,則復制目錄的整個內容,包括文件系統元數據。

Note

The directory itself is not copied, just its contents.

注意

目錄本身不會被復制,只是其內容被復制。

  • If <src> is any other kind of file, it is copied individually along with its metadata. In this case, if <dest> ends with a trailing slash /, it will be considered a directory and the contents of <src> will be written at <dest>/base(<src>).

  • 如果 <src> 是任何其他類型的文件,則會將其及其元數據一起單獨復制。在這種情況下,如果<dest> 以尾斜杠 / 結尾,則它將被視為目錄,並且 <src> 的內容將被寫在 <dest>/base(<src>) 中。

  • If multiple <src> resources are specified, either directly or due to the use of a wildcard, then <dest> must be a directory, and it must end with a slash /.

  • 如果直接或由於使用通配符而指定了多個 <src> 資源,則 <dest> 必須是目錄,並且必須以斜杠 / 結束。

  • If <dest> does not end with a trailing slash, it will be considered a regular file and the contents of <src> will be written at <dest>.

  • 如果 <dest> 不以斜杠結尾,則將其視為常規文件,並且 <src> 的內容將被寫入<dest>

  • If <dest> doesn’t exist, it is created along with all missing directories in its path.

  • 如果 <dest> 不存在,則會與路徑中所有缺少的目錄一起創建它。

Note

The first encountered COPY instruction will invalidate the cache for all following instructions from the Dockerfile if the contents of <src> have changed. This includes invalidating the cache for RUN instructions. See the Dockerfile Best Practices guide – Leverage build cache for more information.

注意

如果 <src> 的內容已更改,則遇到的第一個 COPY 指令將使 Dockerfile 中所有以下指令的緩存無效。這包括使 RUN 指令的高速緩存無效。有關更多信息,請參見 Dockerfile 最佳實踐指南-利用構建緩存

示例

Dockerfile 文件

FROM busybox
COPY text2.txt /text2/

構建結果

[root@master env]# docker build -t jiangbo:0.0.1 .
Sending build context to Docker daemon  3.584kB
Step 1/2 : FROM busybox
 ---> dc3bacd8b5ea
Step 2/2 : COPY text2.txt /text2/
 ---> 072721a4c4d8
Successfully built 072721a4c4d8
Successfully tagged jiangbo:0.0.1

查看結果

[root@master env]# docker run -it jiangbo:0.0.1
/ # ls
bin    dev    etc    home   proc   root   sys    text2  tmp    usr    var
/ # ls t
text2/  tmp/
/ # ls text2/
text2.txt
/ #

總結

介紹了 Dockerfile 中 COPY 指令的使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM