I have a repository that I would like to build an image for. There are multiple folders in there and I would like to exclude one called 'solver'. Using a dockerignore file is not an option.
I have tried:
COPY ./[(^solver)] ./
COPY ./[(^solver)]* ./
COPY ./[^solver] ./
However these are not working.
The second solution in this post does not solve my problem.
As long as .dockerignore is not an option, there are 3 methods that will work:
Exclude the folder from COPY by multiple copy instructions (one for each letter):
COPY ./file_that_always_exists.txt ./[^s]* . # All files that don't start with 's'
COPY ./file_that_always_exists.txt ./s[^o]* . # All files that start with 's', but not 'so'
COPY ./file_that_always_exists.txt ./so[^l]* . # All files that start with 'so', but not 'sol'
COPY ./file_that_always_exists.txt ./sol[^v]* . # All files that start with 'sol', but not 'solv'
COPY ./file_that_always_exists.txt ./solv[^e]* . # All files that start with 'solv', but not 'solve'
COPY ./file_that_always_exists.txt ./solve[^r]* . # All files that start with 'solve', but not 'solver'
Disadvantage: This will flatten the folders structures, also, imagine that you have more than one folder to do that for :(
Note, the requirement for file_that_always_exists.txt (e.g. can be Dockerfile) is to avoid the error COPY failed: no source files were specified if there are no files that match the copy step.
Copy all folders and then in a different layer delete the unwanted folder:
COPY . .
RUN rm -rf ./solver
Disadvantage: The content of the folder will still be visible in the Docker image, and if you are trying to decrease the image size this will not help.
Manually specify the files and folders you would like to copy (ðŸ˜):
COPY ["./file_to_copy_1.ext", "file_to_copy_2.ext", "file_to_copy_3.ext", "."]
COPY ./folder_to_copy_1/ ./folder_to_copy_1/
# ...
COPY ./folder_to_copy_n/ ./folder_to_copy_n/
Disadvantage: You have to manually write all files and folders, but more annoyingly, manually update the list when the folder hierarchy changes.
Each method has it's own advantages and disadvantages, choose the one that most fit your application requirements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With