Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker COPY all files and folders except some files/folders

Tags:

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'

like image 688
VISHAL DAGA Avatar asked Jan 29 '19 06:01

VISHAL DAGA


People also ask

What does Workdir in docker do?

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.

Does docker copy overwrite directory?

When copying a single file to an existing LOCALPATH, the docker cp command will either overwrite the contents of LOCALPATH if it is a file or place it into LOCALPATH if it is a directory, overwriting an existing file of the same name if one exists. For example, this command: $ docker cp sharp_ptolemy:/tmp/foo/myfile.

Where should Dockerignore file be?

dockerignore file is on the root directory of your context, it will ignore it if it is somewhere in the subfolder.


1 Answers

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

like image 137
Karol Samborski Avatar answered Oct 30 '22 18:10

Karol Samborski