How to copy multiple files to one layer using Dockerfile?

The following Dockerfile contains four COPY layers:

 COPY README.md ./ COPY package.json ./ COPY gulpfile.js ./ COPY __BUILD_NUMBER ./ 

How to copy these files with a single layer? The following has been tested:

 COPY [ "__BUILD_NUMBER ./", "README.md ./", "gulpfile ./", "another_file ./", ] 
+188
dockerfile
May 15 '15 at 9:49
source share
4 answers
 COPY README.md package.json gulpfile.js __BUILD_NUMBER ./ 

or

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

You can also use wildcards in the specification of the source file. See the docs for more details .

The catalogs are special! If you write

 COPY dir1 dir2 ./ 

it actually works like

 COPY dir1/* dir2/* ./ 

If you want to copy several directories (not their contents) to the target directory in one command, you need to configure the build context so that your source directories are under a common parent, and then COPY to parent.

+315
May 19 '15 at 4:08
source share
 COPY <all> <the> <things> <last-arg-is-destination> 

But here is an important excerpt from the documentation:

If you have several Dockerfile steps that use different files from your context, copy them individually, not all at once. This ensures that the build cache of each step will only be invalidated (forcing a step to restart) if specially required files are modified.

https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#add-or-copy

+27
Jul 12 '18 at 11:47
source share

plain

 COPY README.md package.json gulpfile.js __BUILD_NUMBER ./ 

from document

If multiple resources are specified, either directly or because of the use of a wildcard, then there must be a directory and it must end with a slash /.

+6
Feb 02 '18 at 13:35
source share

It may be worth mentioning that you can also create a .dockerignore file to exclude files that you do not want to copy:

https://docs.docker.com/engine/reference/builder/#dockerignore-file

Before the docker command-line interface sends context to the docker daemon, it looks for a file named .dockerignore in the context root. If this file exists, the CLI modifies the context to exclude files and directories that match the patterns in it. This helps to avoid unnecessarily sending large or confidential files and directories to the daemon and potentially adding them to images using ADD or COPY.

+1
Aug 20 '19 at 14:04
source share



All Articles