COPY with docker but with exclusion

2019-03-09 10:43发布

In a Dockerfile, I have

COPY . .

I want to exclude an entire directory, in my case, node_modules directory.

Something like this:

   COPY [all but **/node_modules/**] .

Is this possible with Docker?

3条回答
神经病院院长
2楼-- · 2019-03-09 10:57

Adding .dockerignore works for me. One additional point Those who are trying this solution on Windows , windows will not let you create .dockerignore file (as it doesn't by default allows creating file starting with .)

the trick to that is mentioned here : https://www.hanselman.com/blog/HowToCreateAFileWithADotPrefixInWindowsExplorer.aspx

which is include an ending dot also, like : .dockerignore. and hit enter ( provided you have enabled view extension options from folder options )

查看更多
手持菜刀,她持情操
3楼-- · 2019-03-09 10:59

Create file .dockerignore in your docker build context directory (so in this case, most likely a directory that is a parent to node_modules) with one line in it:

**/node_modules

although you probably just want:

node_modules

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

查看更多
你好瞎i
4楼-- · 2019-03-09 11:01

For those who can't use a .dockerignore file (e.g. if you need the file in one COPY but not another):

Yes, but you need multiple COPY instructions. Specifically, you need a COPY for each letter in the filename you wish to exclude.

COPY [^n]*    # All files that don't start with 'n'
COPY n[^o]*   # All files that start with 'n', but not 'no'
COPY no[^d]*  # All files that start with 'no', but not 'nod'

Continuing until you have the full file name, or just the prefix you're reasonably sure won't have any other files.

查看更多
登录 后发表回答