.dockerignore and .gitignore have similar names and similar writing styles, so I thought I should write them in the same way, but they were different. We'll look at the differences in this article.
People who have written .gitignore but haven't written much .dockerignore
The .gitignore specification can be found on the official gitignore page (https://git-scm.com/docs/gitignore). In Japanese, Qiita's Detailed explanation of .gitignore specifications is easy to understand.
The specifications of .dockerignore are described in docker build #dockerignore on the official page.
There are three main rules.
--The Go language filepath.Match rule is used for pattern matching. --Supports special wildcard strings
**
. This matches multiple directories (including zero). --If you start writing the beginning of a line with an exclamation mark!
, It will specify an exception to the exclusion.
Before explaining the difference between .dockerignore and .gitignore, let's talk about file transfer and the role of .dockerignore during docker build.
When building a Dockerfile, docker tars the build context and below. This is to transfer the target directory to the docker daemon. This tar also contains files that are not COPY or ADD. It contains all the files under the build context.
.dockerignore
Files that are not needed for docker builds (that you don't want to put in the tar) are listed in .dockerignore. Creating .dockerignore has the effect of shortening the build time, optimizing the docker image size, and preventing unintentional leakage of confidential information (passwords, etc.).
The build context root is the path used by docker build. (Not the location of the Dockerfile)
docker build -f path/to/Dockerfile myprj
-----
↑ The path specified in the argument of docker build is the build context root ↑
In the example above, myprj
is the build context root. In the above example, .dockerignore would be placed in the myprj
directory.
Now the main subject. .dockerignore and .gitignore are similar in purpose and writing, but not in the same implementation. The specifications are also different.
.gitignore
In .gitignore, the written file or directory name is ignored in any hierarchy below the .gitignore file.
For example
.gitignore
target
If you write
target
src/target
path/a/b/target
Etc. are ignored.
.dockerignore
In .dockerignore, all paths are relative to the path where .dockerignore is located. [^ dockerignore_path]
[^ dockerignore_path]: You can also prefix the path with /
as with .gitignore.
For example
.dockerignore
target
If you write
target
Only ignored,
src/target
path/a/b/target
Is not ignored.
If you want to target any hierarchy like .gitignore
dockerignore
**/target
It is described as.
.gitignore
For .gitignore, you can also place .gitignore in a subdirectory. In that case, the rule closer to the target file has priority.
.dockerignore
.dockerignore only loads .dockerignore in the build context root. .Dcokerignore files located in subdirectories will not be read.
Be careful when the Dockerfile directory and build context are different. Instead of putting the .gitignore in the same location as the Dockerfile, put the .dockerignore in the build context.
Maybe there's a little more difference, but so far I've found this much. Have a comfortable docker life.
-Dockerfile Reference — Docker-docs-ja 17.06 Document # dockerignore-file -build — Docker-docs-ja 17.06 Document #dockerignor -[Dockerfile writing best practices | Docker documentation #Understanding the build context](https://matsuand.github.io/docs.docker.jp.onthefly/develop/develop-images/dockerfile_best-practices/#%E3%83 % 93% E3% 83% AB% E3% 83% 89% E3% 82% B3% E3% 83% B3% E3% 83% 86% E3% 82% AD% E3% 82% B9% E3% 83% 88 % E3% 81% AE% E7% 90% 86% E8% A7% A3) -Three points for improving lead time in Docker Build | PLAID engineer blog -docker-compose doesn't start build at all, or doesn't start. It starts when you forget it. --Qiita
Recommended Posts