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
.dockerignore not respected when copying from builder in multi stage builds #33923
Comments
Yes, |
@tonistiigi Thanks for the answer. Since multi stage builds are used often for this purpose, of cloning some private git repository, and copying it after some modification to the next stage, shouldn't COPY support .dockerignore between stages as well? I understand it's a matter of opinion, but it seems cleaner somehow to me that COPY command between stages should support the same option. |
I agree that this is a common use-case but don't think |
Is there any solution to this already? |
Possibly #32507 ( FROM foo AS builder
RUN <your git clone etc.> /source
RUN rm -r /source/.git
FROM bar AS finalstage
COPY --from=builder /source /somewhere |
Related: #37333 |
I don't believe that's coinicidental at all, @tonistiigi. I was very surprised to discover that Especially given the botched up syntax and semantics of the The current "solution" either involves hundreds of
I would be fine with something like this: COPY --use-dockerignore <source> <target> Or this: RM --use-dockerignore ./ Having to repeat every statement inside the I understand how using the |
Hello guys, it's 2021 and this issue is still not resolved :/ Below my workaround that honor multiple .dockerignore in any path location in project between stages. This need to be added at the end of each stage, where you have unneeded artifacts: Please not here that it can delete host files if you mount project files by using volume mechanism! Also the syntax used by docker ignore and the syntax used by extended globs are not compatible in some aspects. eg: Also you need to have a bash in your image, at least in version 4. |
I guess you mean when using |
@hetii thx for this RUN command to cleanup before next stage. I got a syntax error because of a missing |
Hate to be that guy but do you happen to know of the equivalent command in |
Is running a script to delete files according to the dockerignore still the only way to do this? |
Adding to the case. Same as the others. I would want COPY to support the docker ignore between multi stage in order to be able to optimize the image size. I have multi stage build on a mono repo with X applications and libraries in TS. So, to build a production image I require my dev dependencies to be installer in order to transpile. Once it's done, I want to leave behind the dependencies. And reinstall only the production ones. Without the requested feature, we need to add X rm commands to clean the src folder. Which could be a tedious one time task but is not. Everytime you'll add or remove a new app/lib, you'll have to figure out the new rm commands. A support for docker ignore is more flexible than any other solution imho. A side note: NPM can |
Still a problem for me too! |
If you have the possibility to download the repository as an .tar.gz archive, a possible solution is that the tar command accept a As an example: FROM debian:bookworm-slim as builder
WORKDIR /tmp
# Download release from Github
ADD https://github.com/user/repository/archive/refs/tags/myrelease.tar.gz .
# Extract from tar archive excluding files according to the .dockerignore file
RUN --mount=type=bind,source=.dockerignore,target=.dockerignore \
tar -xf myrelease.tar.gz --exclude-from=.dockerignore
FROM python:${PYTHON_VERSION}-slim as base
WORKDIR /app
# Copy the source code into the container.
COPY --from=builder /tmp/ . Make sure that your .dockerignore file doesn't include itself, otherwise you will not be able to mount it.
|
For those looking for exclusion patterns when copying between stages, a pull-request was merged in the Dockerfile syntax that introduces a documentation: It's currently in the "labs" variant of the Dockerfile syntax, and requires a |
I use multi stage builds as a way to manage image size & data that I do not wish to make its way into the final image.
The first image downloads some private git repositories, using injected ssh key, and the second copies the repos into the final image without the .git data.
Unfortunately, it does not look like .dockerignore is respected in
COPY --from=builder
commands, and as result the final image still has the .git folder with all the data inside.The text was updated successfully, but these errors were encountered: