16

Inside the dockerfile, I want to specify a copy operation for files which are defined by globbing and I want it to be copied with the path as well. So, something like this:

COPY ./src/**/project.json /app/**/

Considering I have the following structure:

./src/bar/project.json
./src/foo/project.json

The destination should look like this:

/app/bar/project.json
/app/foo/project.json

but apparently, this doesn't work and I really don't want to specify all of the COPY operations separately if I have a chance. Any idea how to do this?

Note that I cannot basically ignore other files through .dockerignore as suggested as I am going to copy the other files from the same folder after ruining a package install operation. So, the dockerfile is similar to this:

FROM microsoft/aspnet:1.0.0-rc1-update1

COPY ./src/**/project.json /app/**/
WORKDIR /app/ModernShopping.Auth
RUN ["dnu", "restore"]
ADD ./src /app

EXPOSE 44300
ENTRYPOINT ["dnx", "web"]
Community
  • 1
  • 1
tugberk
  • 57,477
  • 67
  • 243
  • 335
  • seems like this is not there yet: https://github.com/docker/docker/issues/15858 – tugberk Feb 27 '16 at 19:56
  • Got the same sort of issue here hope they add it soon: https://github.com/docker/docker/issues/15771#issuecomment-225786238 – Joel Harkes Mar 30 '17 at 07:26
  • 1
    Why do you not do a three way copy? First copy everything to some temp folder with COPY. Then you do `RUN yourcopycommand only-my-proj-files-from-temp`, next is `RUN dnu restore` and afterwards another `RUN yourcopycommand the-remaining-files-from-temp`. Last is temp folder cleanup. – blacklabelops Aug 20 '17 at 20:40
  • Does this answer your question? [Docker COPY with folder wildcards](https://stackoverflow.com/questions/45786035/docker-copy-with-folder-wildcards) – Michael Freidgeim Nov 28 '20 at 23:17

3 Answers3

8

For any non-standard build operation, I prefer wrapping the docker build command in a script (named 'build').
Here I would

  • create a subfolder tmp (just beside the Dockerfile, in order to keep it in the docker build context)
  • make the shell cp with globing: cp ./src/**/project.json tmp
  • call docker build, with a Dockerfile including COPY tmp/ /app/
  • deleting tmp.

That way, I pre-configure what I need from host, before building the image from the host context.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
6

Workaround

Dockerfile:

COPY src/ /app/

.dockerignore:

**
!**/project.json
tugberk
  • 57,477
  • 67
  • 243
  • 335
Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124
  • 8
    The downside is that I will be copyig everything else after another operation (`dnu restore`). So, `.dockerignore` will be a problem there. – tugberk Feb 27 '16 at 16:17
  • Cool workaround for what I asked. I should have made the question more clear, updated it now. – tugberk Feb 27 '16 at 20:01
3

Little late, but with multistage building, you can get this behavior. Here I do it for a large maven build

FROM maven:3.6-slim as staging

WORKDIR /src/
COPY . .
RUN mkdir /poms/ \
    && find ./ -type d -exec mkdir -p '/poms/{}' \; \
    && find ./ -name pom.xml -exec cp -r '{}' '/poms/{}' \;

FROM maven:3.6-slim as builder

WORKDIR /src/

COPY --from=staging /poms/* ./
RUN mvn dependency:go-offline

A bit of a hack, but given that it's a multistage, the first stage just get's thrown away and the copy is a cheap operation

  • 3
    I was using a similar solution to this. The problem with it is it would repeat the dependency goal for every code change (it would not be cached) – Miguel Alorda Feb 28 '22 at 07:22