How to copy multiple files in one layer using a Dockerfile?


The following Dockerfile contains four COPY layers:

COPY package.json ./
COPY gulpfile.js ./

How to copy these files using one layer instead? The following was tried:

    "__BUILD_NUMBER ./",
    " ./",
    "gulpfile ./",
    "another_file ./",

This question is tagged with dockerfile

~ Asked on 2015-05-15 09:49:49

The Best Answer is


COPY package.json gulpfile.js __BUILD_NUMBER ./


COPY ["__BUILD_NUMBER", "", "gulpfile", "another_file", "./"]

You can also use wildcard characters in the sourcefile specification. See the docs for a little more detail.

Directories are special! If you write

COPY dir1 dir2 ./

that actually works like

COPY dir1/* dir2/* ./

If you want to copy multiple directories (not their contents) under a destination directory in a single command, you'll need to set up the build context so that your source directories are under a common parent and then COPY that parent.

~ Answered on 2015-05-19 04:08:22


COPY <all> <the> <things> <last-arg-is-destination>

But here is an important excerpt from the docs:

If you have multiple Dockerfile steps that use different files from your context, COPY them individually, rather than all at once. This ensures that each step’s build cache is only invalidated (forcing the step to be re-run) if the specifically required files change.

~ Answered on 2018-07-12 11:47:28

Most Viewed Questions: