Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 using one layer instead? The following was tried:

COPY [     "__BUILD_NUMBER ./",     "README.md ./",     "gulpfile ./",     "another_file ./", ] 
like image 943
kazhuravlev Avatar asked May 15 '15 09:05

kazhuravlev


People also ask

Can Dockerfile have multiple Workdir?

you can have multiple WORKDIR in same Dockerfile. If a relative path is provided, it will be relative to the previous WORKDIR instruction. If no WORKDIR is specified in the Dockerfile then the default path is / . The WORKDIR instruction can resolve environment variables previously set in Dockerfile using ENV.

How do I COPY in Dockerfile?

Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container. The second parameter of the docker copy command is the location to save the file on the host.

Can a Dockerfile build multiple images?

In a multistage build, you have a single Dockerfile, but can define multiple images inside it to help build the final image. In this post, you'll learn about the core concepts of multistage builds in Docker and how they help to create production-grade images.

What does Workdir do in Dockerfile?

The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.


2 Answers

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

or

COPY ["__BUILD_NUMBER", "README.md", "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.

like image 147
Nathaniel Waisbrot Avatar answered Dec 06 '22 07:12

Nathaniel Waisbrot


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.

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

like image 29
swapsCAPS Avatar answered Dec 06 '22 05:12

swapsCAPS