add support for exclusion rules in dockerignore

Signed-off-by: Dave Goodchild <buddhamagnet@gmail.com>
This commit is contained in:
buddhamagnet 2015-04-09 20:07:06 +01:00 committed by Tibor Vass
parent fcf01194a9
commit 63193b17b1
2 changed files with 84 additions and 82 deletions

View File

@ -41,10 +41,11 @@ whole context must be transferred to the daemon. The Docker CLI reports
> repository, the entire contents of your hard drive will get sent to the daemon (and > repository, the entire contents of your hard drive will get sent to the daemon (and
> thus to the machine running the daemon). You probably don't want that. > thus to the machine running the daemon). You probably don't want that.
In most cases, it's best to put each Dockerfile in an empty directory, and then add only In most cases, it's best to put each Dockerfile in an empty directory. Then,
the files needed for building that Dockerfile to that directory. To further speed up the only add the files needed for building the Dockerfile to the directory. To
build, you can exclude files and directories by adding a `.dockerignore` file to the same increase the build's performance, you can exclude files and directories by
directory. adding a `.dockerignore` file to the directory. For information about how to
[create a `.dockerignore` file](#the-dockerignore-file) on this page.
You can specify a repository and tag at which to save the new image if You can specify a repository and tag at which to save the new image if
the build succeeds: the build succeeds:
@ -169,43 +170,67 @@ will result in `def` having a value of `hello`, not `bye`. However,
`ghi` will have a value of `bye` because it is not part of the same command `ghi` will have a value of `bye` because it is not part of the same command
that set `abc` to `bye`. that set `abc` to `bye`.
## The `.dockerignore` file ### .dockerignore file
If a file named `.dockerignore` exists in the source repository, then it If a file named `.dockerignore` exists in the root of `PATH`, then Docker
is interpreted as a newline-separated list of exclusion patterns. interprets it as a newline-separated list of exclusion patterns. Docker excludes
Exclusion patterns match files or directories relative to the source repository files or directories relative to `PATH` that match these exclusion patterns. If
that will be excluded from the context. Globbing is done using Go's there are any `.dockerignore` files in `PATH` subdirectories, Docker treats
them as normal files.
Filepaths in `.dockerignore` are absolute with the current directory as the
root. Wildcards are allowed but the search is not recursive. Globbing (file name
expansion) is done using Go's
[filepath.Match](http://golang.org/pkg/path/filepath#Match) rules. [filepath.Match](http://golang.org/pkg/path/filepath#Match) rules.
> **Note**: You can specify exceptions to exclusion rules. To do this, simply prefix a
> The `.dockerignore` file can even be used to ignore the `Dockerfile` and pattern with an `!` (exclamation mark) in the same way you would in a
> `.dockerignore` files. This might be useful if you are copying files from `.gitignore` file. Currently there is no support for regular expressions.
> the root of the build context into your new container but do not want to Formats like `[^temp*]` are ignored.
> include the `Dockerfile` or `.dockerignore` files (e.g. `ADD . /someDir/`).
The following example shows the use of the `.dockerignore` file to exclude the The following is an example `.dockerignore` file:
`.git` directory from the context. Its effect can be seen in the changed size of
the uploaded context. ```
*/temp*
*/*/temp*
temp?
*.md
!LICENCSE.md
```
This file causes the following build behavior:
| Rule | Behavior |
|----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `*/temp*` | Exclude all files with names starting with`temp` in any subdirectory below the root directory. For example, a file named`/somedir/temporary.txt` is ignored. |
| `*/*/temp*` | Exclude files starting with name `temp` from any subdirectory that is two levels below the root directory. For example, the file `/somedir/subdir/temporary.txt` is ignored. |
| `temp?` | Exclude the files that match the pattern in the root directory. For example, the files `tempa`, `tempb` in the root directory are ignored. |
| `*.md ` | Exclude all markdown files. |
| `!LICENSE.md` | Exception to the exclude all Markdown files is this file, `LICENSE.md`, include this file in the build. |
The placement of `!` exception rules influences the matching algorithm; the
last line of the `.dockerignore` that matches a particular file determines
whether it is included or excluded. In the above example, the `LICENSE.md` file
matches both the `*.md` and `!LICENSE.md` rule. If you reverse the lines in the
example:
```
*/temp*
*/*/temp*
temp?
!LICENCSE.md
*.md
```
The build would exclude `LICENSE.md` because the last `*.md` rule adds all
Markdown files back onto the ignore list. The `!LICENSE.md` rule has no effect
because the subsequent `*.md` rule overrides it.
You can even use the `.dockerignore` file to ignore the `Dockerfile` and
`.dockerignore` files. This is useful if you are copying files from the root of
the build context into your new container but do not want to include the
`Dockerfile` or `.dockerignore` files (e.g. `ADD . /someDir/`).
$ docker build .
Uploading context 18.829 MB
Uploading context
Step 0 : FROM busybox
---> 769b9341d937
Step 1 : CMD echo Hello World
---> Using cache
---> 99cc1ad10469
Successfully built 99cc1ad10469
$ echo ".git" > .dockerignore
$ docker build .
Uploading context 6.76 MB
Uploading context
Step 0 : FROM busybox
---> 769b9341d937
Step 1 : CMD echo Hello World
---> Using cache
---> 99cc1ad10469
Successfully built 99cc1ad10469
## FROM ## FROM

View File

@ -653,6 +653,26 @@ If you use STDIN or specify a `URL`, the system places the contents into a
file called `Dockerfile`, and any `-f`, `--file` option is ignored. In this file called `Dockerfile`, and any `-f`, `--file` option is ignored. In this
scenario, there is no context. scenario, there is no context.
By default the `docker build` command will look for a `Dockerfile` at the
root of the build context. The `-f`, `--file`, option lets you specify
the path to an alternative file to use instead. This is useful
in cases where the same set of files are used for multiple builds. The path
must be to a file within the build context. If a relative path is specified
then it must to be relative to the current directory.
In most cases, it's best to put each Dockerfile in an empty directory. Then, add
to that directory only the files needed for building the Dockerfile. To increase
the build's performance, you can exclude files and directories by adding a
`.dockerignore` file to that directory as well. For information on creating one,
see the [.dockerignore file](../../reference/builder/#dockerignore-file).
If the Docker client loses connection to the daemon, the build is canceled.
This happens if you interrupt the Docker client with `ctrl-c` or if the Docker
client is killed for any reason.
> **Note:** Currently only the "run" phase of the build can be canceled until
> pull cancelation is implemented).
### Return code ### Return code
On a successful build, a return code of success `0` will be returned. On a successful build, a return code of success `0` will be returned.
@ -673,55 +693,11 @@ INFO[0000] The command [/bin/sh -c exit 13] returned a non-zero code: 13
$ echo $? $ echo $?
1 1
``` ```
### .dockerignore file
If a file named `.dockerignore` exists in the root of `PATH` then it
is interpreted as a newline-separated list of exclusion patterns.
Exclusion patterns match files or directories relative to `PATH` that
will be excluded from the context. Globbing is done using Go's
[filepath.Match](http://golang.org/pkg/path/filepath#Match) rules.
Please note that `.dockerignore` files in other subdirectories are
considered as normal files. Filepaths in `.dockerignore` are absolute with
the current directory as the root. Wildcards are allowed but the search
is not recursive.
#### Example .dockerignore file
*/temp*
*/*/temp*
temp?
The first line above `*/temp*`, would ignore all files with names starting with
`temp` from any subdirectory below the root directory. For example, a file named
`/somedir/temporary.txt` would be ignored. The second line `*/*/temp*`, will
ignore files starting with name `temp` from any subdirectory that is two levels
below the root directory. For example, the file `/somedir/subdir/temporary.txt`
would get ignored in this case. The last line in the above example `temp?`
will ignore the files that match the pattern from the root directory.
For example, the files `tempa`, `tempb` are ignored from the root directory.
Currently there is no support for regular expressions. Formats
like `[^temp*]` are ignored.
By default the `docker build` command will look for a `Dockerfile` at the
root of the build context. The `-f`, `--file`, option lets you specify
the path to an alternative file to use instead. This is useful
in cases where the same set of files are used for multiple builds. The path
must be to a file within the build context. If a relative path is specified
then it must to be relative to the current directory.
If the Docker client loses connection to the daemon, the build is canceled.
This happens if you interrupt the Docker client with `ctrl-c` or if the Docker
client is killed for any reason.
> **Note:** Currently only the "run" phase of the build can be canceled until
> pull cancelation is implemented).
See also: See also:
[*Dockerfile Reference*](/reference/builder). [*Dockerfile Reference*](/reference/builder).
#### Examples ### Examples
$ docker build . $ docker build .
Uploading context 10240 bytes Uploading context 10240 bytes
@ -790,7 +766,8 @@ affect the build cache.
This example shows the use of the `.dockerignore` file to exclude the `.git` This example shows the use of the `.dockerignore` file to exclude the `.git`
directory from the context. Its effect can be seen in the changed size of the directory from the context. Its effect can be seen in the changed size of the
uploaded context. uploaded context. The builder reference contains detailed information on
[creating a .dockerignore file](../../builder/#dockerignore-file)
$ docker build -t vieux/apache:2.0 . $ docker build -t vieux/apache:2.0 .