-
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
Feature request: On Dockerfiles, allow filepaths to be excluded from ADD and COPY commands #4439
Comments
What is the benefit of that? We should not spend time transferring files to the daemon where they would be excluded by the copy step later. This doesn't mean that copy side shouldn't filter at all (copies between stages, multiple copies with different filters) but it should happen both for local source transfer and for copy. I'm not so sure about the proposed syntax. It looks like it can become quite verbose if used as a replacement for a dockerignore. Maybe some way to define a new stage based on another filter with an additional filter definition, where the filter has multiple rules. Not sure exactly but #4239 also goes for a way to define new stages. It is also possible that this case is different enough from the dockerignore case and there will not be too many rules. Some other considerations:
|
Thx @tonistiigi for reviewing this issue. I can see that there were other proposals over time, in addition to the one you linked, such as: And this is a very similar proposal (meaning the current issue is actually a duplicate) I mentioned the context in the original text, but in retrospect it was probably a mistake, as I confess I don't understand in details how the context works. The main target for this feature is actually the build cache. |
I think a generic FROM myimage AS not_py
COPY --filter=!*.py sources /src
FROM myimage AS not_cpp_or_txt
# OR
COPY --filter=!*.cpp --filter=!*.txt sources /src
FROM myimage AS combine
# AND
COPY --filter=!*.cpp,!*.py sources /src
FROM myimage AS only_cpp
COPY --filter=*.cpp sources /src
FROM myimage AS all
# default behavior as if we don't set --filter flag
COPY --filter=** sources /src For more control we can use a comma separated list of match object instead of a simple path glob ( |
and
👉 This is just a first "quick" blurb, but I know there's been various ticket around this (or similar) proposals, and we may have to go through use-cases mentioned in those.
I definitely think this can be a concern. Using a |
This is my use case 🙋♂️ Specifically, I'm generating some "clean" versions of my My Dockerfile looks something like this: FROM public.ecr.aws/lambda/nodejs:20.2023.11.21.13
RUN dnf install -y gcc gcc-c++ git make openssl-devel tar zip
WORKDIR /var/task
# This layer can be cached even between version bumps (or any other metadata changes that doesn't affect deps)
RUN --mount=type=bind,source=clean-package.json,target=package.json --mount=type=bind,source=clean-package-lock.json,target=package-lock.json npm ci
# These two layers would be condensed to one:
COPY . .
RUN rm clean-package.json clean-package-lock.json
RUN npm run-script build For me, it would be great with a simple COPY --exclude=clean-package.json --exclude=clean-package-lock.json . . This is not meant to replace |
We discussed this in one of the moby maintainers meetings. Most people prefered the exclude over filter/include to cover the most basic cases. And possibly extend this later if reusing same filters becomes a problem. But Regarding implementation:
|
I believe so. That is the intent. |
If I understood properly, this mean that a file would need not be added to the context if it's ignored by In this case, a file which is excluded (via I wonder if for this specific use case simply using I've created a change for the second suggestion (the cache) on #4561 and would be happy to get some feedback. Thank you in advance. |
and from stages in use. Dockerfile frontend will already determine if a stage is reachable for a given build.
Yes, there are cases where we can't be super precise about it. I think if there are multiple active copies, one has exclude and another one is using the excluded files then we should still transfer with one context and ignore the excludes (let the excludes happen via copy). But for simple cases where there is only one copy or all excludes are the same it should be possible to do the filter on transfer. I'm not sure how far we should go in determining if exclude from one |
I am continuing the discussion in the proposed PR #4561 |
It exposes the `ExcludePatterns` to the Dockerfile frontend, adding `--exclude=<pattern>` option in the COPY and ADD commands, which will cause filepaths matching such patterns not to be copied. `--exclude` can be used multiple times. References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
This affects the --exclude option in the COPY and ADD commands on Dockerfiles. References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
It exposes the `ExcludePatterns` to the Dockerfile frontend, adding `--exclude=<pattern>` option in the COPY and ADD commands, which will cause filepaths matching such patterns not to be copied. `--exclude` can be used multiple times. References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
This affects the --exclude option in the COPY and ADD commands on Dockerfiles. References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
It exposes the `ExcludePatterns` to the Dockerfile frontend, adding `--exclude=<pattern>` option in the COPY and ADD commands, which will cause filepaths matching such patterns not to be copied. `--exclude` can be used multiple times. References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
This affects the --exclude option in the COPY and ADD commands on Dockerfiles. References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
It exposes the `ExcludePatterns` to the Dockerfile frontend, adding `--exclude=<pattern>` option in the COPY and ADD commands, which will cause filepaths matching such patterns not to be copied. `--exclude` can be used multiple times. References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
This affects the --exclude option in the COPY and ADD commands on Dockerfiles. References moby#4439 Signed-off-by: Leandro Santiago <[email protected]>
This feature will be useful for multi stage builds, where some files should be copied in some stages but not others.
I imagine it looking like it follows:
In such example,
/sources
has some files required bynot_py
but not bynot_cpp_or_txt
, and vice-versa.As far as I am aware, such construct at the moment cannot be accomplished with
.dockerignore
, which seems to prevent files to be copied to the context, whereas theexclude
proposed adds files to the context, but not to the commands that use it.The text was updated successfully, but these errors were encountered: