【Docker】.dockerignore 檔案
參考教程:https://docs.docker.com/engine/reference/builder/
環境
- virtual box 6.1
- centos 7.8
- 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
COPY
.
在 docker CLI 將上下文傳送到 docker 守護程式之前,它將在上下文的根目錄中查詢名為 .dockerignore
的檔案。如果此檔案存在,則 CLI 會修改上下文以排除與其中的模式匹配的檔案和目錄。這有助於避免不必要地將較大或敏感的檔案和目錄傳送到守護程式,並避免使用 ADD
或 COPY
將它們新增到映象中。
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
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/bar
和 foo/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
檔案。這些檔案仍被髮送到守護程式,因為它需要它們來完成其工作。但是,ADD
和 COPY
指令不會將它們複製到映象中。
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
檔案的作用以及使用方式。