1

Assume I have the following dir structure:

languages
    -en-GB
      --page1.json
      --page2.json

    -fr-FR
      --page1.json
      --page2.json

Now let's assume I want to copy the folder structure, but only page1.json content:

I've tried this:

COPY ["languages/**/*page1.json", "./"]

Which results in the folders being copied, but no files.

What I want to end up with is

languages
    -en-GB
      --page1.json

    -fr-FR
      --page1.json

Copied into my image

Alex
  • 37,502
  • 51
  • 204
  • 332
  • alex when I use your copy command it fails with this error: "When using COPY with more than one source file, the destination must be a directory and end with a /" and if you specify a destination path the files overwrite each other because their names are the same. can you copy all the files and in the Dockerfile use "find" command and remove the files you won't? – badger Oct 16 '20 at 14:24
  • @hatefAlipoor sorry, yes -see updated question - `./` – Alex Oct 16 '20 at 14:27
  • I wanted to say COPY ["languages", "."] and then RUN find ./languages -type f -not -name "*page1.json" -exec rm -rf {} \; – badger Oct 16 '20 at 14:49

1 Answers1

2

I am not sure you can use wildcards to produce the filtered result you are looking for.

I believe there are at least two clean and clear ways to achieve this:

Option 1: Copy everything, and cleanup later:

FROM alpine
WORKDIR /languages
COPY languages .
RUN rm -r **/page2.json

Option 2: Add files you don't want into your .dockerignore

# .dockerignore
languages/**/page*.json
!languages/**/page1.json

Option 3: Copy all to a temporary directory, and copy what you need from inside the container using more flexible tools

FROM alpine
WORKDIR /languages
COPY languages /tmp/langs
RUN cd /tmp/langs ; find -name 'page1.json' -exec cp --parents {} /languages \; 
CMD ls -lR /languages
DannyB
  • 12,810
  • 5
  • 55
  • 65
  • If this is to build different images from different Dockerfiles, you could take advantage of the custom per image dockerignore: https://stackoverflow.com/questions/40904409/how-to-specify-different-dockerignore-files-for-different-builds-in-the-same-pr – Zeitounator Oct 16 '20 at 14:46
  • Problem is "page2.json" is just one example... There's many - that could change – Alex Oct 16 '20 at 15:35
  • I updated the answer with option 3, which might be more suitable for this case - although I feel like it might be better to solve the root cause. If these files don't need to be in the container, perhaps they should not be in the working directory – DannyB Oct 16 '20 at 16:05
  • I also updated option 2 - the `.dockerignore` way - to ignore everything except `page1.json` - perhaps this one suits you best? – DannyB Oct 16 '20 at 16:21