-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dockerfile: Allow files to be excluded from cache on COPY and ADD commands #4561
Merged
tonistiigi
merged 7 commits into
moby:master
from
leandrosansilva:feature/exclude-pattern-on-dockerfile
Feb 24, 2024
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
42a0c9c
Dockerfile frontend: expose exclude keyword to ADD and COPY commands
leandrosansilva e897dc6
Dockerfile: Document exclude patterns on COPY and ADD commands
leandrosansilva 398423f
Integration test for Dockerfile --exclude option
leandrosansilva 516130a
Add llb.WithExcludePatterns
leandrosansilva fc23da5
Move Dockerfile copy/add --exclude implementation to Labs
leandrosansilva 0befd8f
Dockerfile frontend: add Integration test for context from git
leandrosansilva 2167e90
dockerfile: add excludepatterns feature to labs
tonistiigi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
frontend/dockerfile/dockerfile2llb/exclude_patterns_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//go:build dfexcludepatterns | ||
// +build dfexcludepatterns | ||
|
||
package dockerfile2llb | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/moby/buildkit/util/appcontext" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDockerfileCopyExcludePatterns(t *testing.T) { | ||
df := `FROM scratch | ||
COPY --exclude=src/*.go --exclude=tmp/*.txt dir /sub/ | ||
` | ||
_, _, _, err := Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{}) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestDockerfileAddExcludePatterns(t *testing.T) { | ||
df := `FROM scratch | ||
ADD --exclude=src/*.go --exclude=tmp/*.txt dir /sub/ | ||
` | ||
_, _, _, err := Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{}) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1138,8 +1138,8 @@ RUN apt-get update && apt-get install -y ... | |
ADD has two forms: | ||
|
||
```dockerfile | ||
ADD [--chown=<user>:<group>] [--chmod=<perms>] [--checksum=<checksum>] <src>... <dest> | ||
ADD [--chown=<user>:<group>] [--chmod=<perms>] ["<src>",... "<dest>"] | ||
ADD [--chown=<user>:<group>] [--chmod=<perms> [--exclude=<exclude>]... [--checksum=<checksum>] <src>... <dest> | ||
ADD [--chown=<user>:<group>] [--chmod=<perms> [--exclude=<exclude>]... ["<src>",... "<dest>"] | ||
``` | ||
|
||
The latter form is required for paths containing whitespace. | ||
|
@@ -1158,20 +1158,25 @@ The latter form is required for paths containing whitespace. | |
> Only octal notation is currently supported. Non-octal support is tracked in | ||
> [moby/buildkit#1951](https://github.com/moby/buildkit/issues/1951). | ||
|
||
> **Note** | ||
> | ||
> The `--exclude` option can be specified multiple times and cause files matching its patterns not to be copied, | ||
> even if the files paths match the pattern specified in `<src>`. This feature requires the build tag `dfexcludepatterns`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "This feature requires using labs channel" cc @dvdksn to double check this before the release. Maybe we need to separate it more. I also see that |
||
|
||
The `ADD` instruction copies new files, directories or remote file URLs from `<src>` | ||
and adds them to the filesystem of the image at the path `<dest>`. | ||
|
||
Multiple `<src>` resources may be specified but if they are files or | ||
directories, their paths are interpreted as relative to the source of | ||
the context of the build. | ||
|
||
Each `<src>` may contain wildcards and matching will be done using Go's | ||
Each `<src>` and `<exclude>` may contain wildcards and matching will be done using Go's | ||
[filepath.Match](https://golang.org/pkg/path/filepath#Match) rules. For example: | ||
|
||
To add all files starting with "hom": | ||
To add all files starting with "hom", excluding all files with `.txt` and `.md` extensions: | ||
|
||
```dockerfile | ||
ADD hom* /mydir/ | ||
ADD --exclude=*.txt --exclude=*.md hom* /mydir/ | ||
``` | ||
|
||
In the example below, `?` is replaced with any single character, e.g., "home.txt". | ||
|
@@ -1366,8 +1371,8 @@ See [`COPY --link`](#copy---link). | |
COPY has two forms: | ||
|
||
```dockerfile | ||
COPY [--chown=<user>:<group>] [--chmod=<perms>] <src>... <dest> | ||
COPY [--chown=<user>:<group>] [--chmod=<perms>] ["<src>",... "<dest>"] | ||
COPY [--chown=<user>:<group>] [--chmod=<perms>] [--exclude=<exclude>]... <src>... <dest> | ||
COPY [--chown=<user>:<group>] [--chmod=<perms>] [--exclude=<exclude>]... ["<src>",... "<dest>"] | ||
``` | ||
|
||
This latter form is required for paths containing whitespace | ||
|
@@ -1380,6 +1385,11 @@ This latter form is required for paths containing whitespace | |
> translating user and group names to IDs restricts this feature to only be viable for | ||
> Linux OS-based containers. | ||
|
||
> **Note** | ||
> | ||
> The `--exclude` option can be specified multiple times and cause files matching its patterns not to be copied, | ||
> even if the files paths match the pattern specified in `<src>`. This feature requires the build tag `dfexcludepatterns`. | ||
|
||
The `COPY` instruction copies new files or directories from `<src>` | ||
and adds them to the filesystem of the container at the path `<dest>`. | ||
|
||
|
@@ -1390,10 +1400,10 @@ of the build. | |
Each `<src>` may contain wildcards and matching will be done using Go's | ||
[filepath.Match](https://golang.org/pkg/path/filepath#Match) rules. For example: | ||
|
||
To add all files starting with "hom": | ||
To add all files starting with "hom", excluding all files with `.txt` and `.md` extensions: | ||
|
||
```dockerfile | ||
COPY hom* /mydir/ | ||
COPY --exclude=*.txt --exclude=*.md hom* /mydir/ | ||
``` | ||
|
||
In the example below, `?` is replaced with any single character, e.g., "home.txt". | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just linter. https://github.com/moby/buildkit/tree/master/frontend/dockerfile/release/labs needs to be updated. Your code and new test does not run atm in the CI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
carried this