VISHAL DAGA
VISHAL DAGA

Reputation: 4289

Docker COPY all files and folders except some files/folders

Dockerfile copy command allows golang regex. But with the regex, I am not able to omit a particular folder.

For example, if the directory has:-

public
dist
webapp
somefile.txt
anotherfile.txt

Now, how should I write the expression for COPY such that it omits 'webapp' and copy all other files and folders?

NOTE: I know I can put it to .dockerignore, but in later build stage in the same Dockerfile, I want to copy that folder - 'webapp'

Upvotes: 10

Views: 26698

Answers (2)

Jared Anderton
Jared Anderton

Reputation: 1026

COPY with exclusions work around

I have a PHP + Node app, with both node_modules and vendor directories, with layer caching in place.

I was looking to exclude my dependencies, by excluding some files from being copied, but since Docker COPY does not support exclusions, I took a different approach, to get my dependencies cached in a different layer.

It took a combination of 3 different steps:

Step 1

Script the tarring the node_modules and vendor directories in my build process:

tar -cf ./node_modules.tgz --directory=./src/node_modules .
tar -cf ./vendor.tgz --directory=./src/vendor . 
docker build ...
rm node_modules.tgz vendor.tgz
docker push ...

Step 2

Use .dockerignore to ignore the node_modules and vendor directories:

src/node_modules
src/vendor

Step 3

Add the tar files to the project, before copying the rest of my source code:

ADD node_modules.tgz /var/www/node_modules
ADD vendor.tgz /var/www/vendor
COPY ./src /var/www

Obviously, the first build is slow while the layer gets cached, and whenever the cache is invalidated (e.g. new packages).

Credit to jason-kane from here for inspiration: https://github.com/moby/moby/issues/15771#issuecomment-207113714

Something else to note: my vendor and node_modules directories are in the same folder as the source code.

Upvotes: 1

Karol Samborski
Karol Samborski

Reputation: 2955

You have two choices:

  1. List all directories you want to copy directly:

    COPY ["foldera", "folderc", "folderd", ..., "/dstPath]

  2. Try to exclude some paths but also make sure that all paths patterns are not including the path we want to exclude:

    COPY ["folder[^b]*", "file*", "/dstPath"]

Also you can read more about available solutions in this issue: https://github.com/moby/moby/issues/15771

Upvotes: 5

Related Questions