【Docker】.dockerignore 文件


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

環境

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

.dockerignore file

Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context. If this file exists, the CLI modifies the context to exclude files and directories that match patterns in it. This helps to avoid unnecessarily sending large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY.

在 docker CLI 將上下文發送到 docker 守護程序之前,它將在上下文的根目錄中查找名為 .dockerignore 的文件。如果此文件存在,則 CLI 會修改上下文以排除與其中的模式匹配的文件和目錄。這有助於避免不必要地將較大或敏感的文件和目錄發送到守護程序,並避免使用 ADDCOPY 將它們添加到鏡像中。

The CLI interprets the .dockerignore file as a newline-separated list of patterns similar to the file globs of Unix shells. For the purposes of matching, the root of the context is considered to be both the working and the root directory. For example, the patterns /foo/bar and foo/bar both exclude a file or directory named bar in the foo subdirectory of PATH or in the root of the git repository located at URL. Neither excludes anything else.

CLI 將 .dockerignore 文件解釋為以換行符分隔的模式列表,類似於 Unix shell 的文件組。為了匹配,上下文的根目錄被認為是工作目錄和根目錄。例如,模式 /foo/barfoo/bar 都排除位於 PATH 的 foo 子目錄中或位於 URL 的 git 倉庫根目錄中名為 bar 的文件或目錄。兩者都不排除其他任何東西。

If a line in .dockerignore file starts with # in column 1, then this line is considered as a comment and is ignored before interpreted by the CLI.

如果 .dockerignore 文件中的行以第 1 列以 號開頭,則該行被視為注釋,並在 CLI 解釋之前被忽略。

Here is an example .dockerignore file:

這是一個示例 .dockerignore 文件:

# comment
*/temp*
*/*/temp*
temp?

This file causes the following build behavior:

此文件導致以下生成行為:

Rule Behavior
# comment Ignored.
*/temp* Exclude files and directories whose names start with temp in any immediate subdirectory of the root. For example, the plain file /somedir/temporary.txt is excluded, as is the directory /somedir/temp.
*/*/temp* Exclude files and directories starting with temp from any subdirectory that is two levels below the root. For example, /somedir/subdir/temporary.txt is excluded.
temp? Exclude files and directories in the root directory whose names are a one-character extension of temp. For example, /tempa and /tempb are excluded.
規則 行為
# comment 忽略
*/temp* 在根目錄的任何直接子目錄中排除名稱以 temp 開頭的文件和目錄。例如,排除了純文件 /somedir/temporary.txt,以及目錄 /somedir/temp
*/*/temp* 從根目錄以下兩級的任何子目錄中排除以 temp 開頭的文件和目錄。例如,排除 /somedir/subdir/temporary.txt
temp? 排除根目錄中名稱為 temp 的一個字符擴展名的文件和目錄。例如,排除 /tempa /tempb

Matching is done using Go’s filepath.Match rules. A preprocessing step removes leading and trailing whitespace and eliminates . and .. elements using Go’s filepath.Clean. Lines that are blank after preprocessing are ignored.

使用 Go 的 filepath.Match 規則進行匹配。預處理步驟使用 Go 的 filepath.Clean 刪除前導和尾隨空格,並消除 ... 元素。預處理后空白的行將被忽略。

Beyond Go’s filepath.Match rules, Docker also supports a special wildcard string ** that matches any number of directories (including zero). For example, **/*.go will exclude all files that end with .go that are found in all directories, including the root of the build context.

除了 Go 的 filepath.Match 規則之外,Docker 還支持特殊的通配符字符串 **,該字符串可匹配任意數量的目錄(包括零個)。例如,**/*.go 將排除在所有目錄(包括構建上下文的根目錄)中找到的所有以 .go 結尾的文件。

Lines starting with ! (exclamation mark) can be used to make exceptions to exclusions. The following is an example .dockerignore file that uses this mechanism:

!(感嘆號)開頭的行可用於排除例外。以下是使用此機制的示例 .dockerignore 文件:

*.md
!README.md

All markdown files except README.md are excluded from the context.

除了 README.md 之外的所有 markdown 文件均從上下文中排除。

The placement of ! exception rules influences the behavior: the last line of the .dockerignore that matches a particular file determines whether it is included or excluded. Consider the following example:

例外規則的位置會影響行為:匹配特定文件的 .dockerignore 的最后一行確定是包含還是排除該文件。考慮以下示例:

*.md
!README*.md
README-secret.md

No markdown files are included in the context except README files other than README-secret.md.

忽略除了 README 開頭的所有 .md 文件,但是 README-secret.md 文件例外。

Now consider this example:

現在考慮以下示例:

*.md
README-secret.md
!README*.md

All of the README files are included. The middle line has no effect because !README*.md matches README-secret.md and comes last.

忽略除了 README 開頭的所有 .md 文件,README-secret.md 文件也會忽略。

You can even use the .dockerignore file to exclude the Dockerfile and .dockerignore files. These files are still sent to the daemon because it needs them to do its job. But the ADD and COPY instructions do not copy them to the image.

您甚至可以使用 .dockerignore 文件來排除 Dockerfile.dockerignore 文件。這些文件仍被發送到守護程序,因為它需要它們來完成其工作。但是,ADDCOPY 指令不會將它們復制到鏡像中。

Finally, you may want to specify which files to include in the context, rather than which to exclude. To achieve this, specify * as the first pattern, followed by one or more ! exception patterns.

最后,您可能想要指定要包含在上下文中的文件,而不是要排除的文件。為此,將 * 指定為第一個模式,然后指定一個或多個 ! 異常模式。

Note

For historical reasons, the pattern . is ignored.

注意

由於歷史原因,模式 . 將被忽略。

練習

建立 Dockerfile

FROM busybox
COPY tex*.txt /

建立 .dockerignore 文件

[root@master env]# cat .dockerignore
text.txt

文件目錄結構

[root@master env]# tree .
.
├── Dockerfile
├── text2.txt
└── text.txt

0 directories, 3 files

構建鏡像並查看結果

[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 tex*.txt /
 ---> 53183cf1cf16
Successfully built 53183cf1cf16
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.txt  tmp        usr        var
/ #

總結

介紹了 Dockerfile 中 .dockerignore 文件的作用以及使用方式。


免責聲明!

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



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