在Dockerfile中,我有以下内容
COPY . .
我想排除一个完整的目录,例如在我的情况下是node_modules目录。类似于这样:
COPY [all but **/node_modules/**] .
使用Docker可以实现这个吗?
在Dockerfile中,我有以下内容
COPY . .
我想排除一个完整的目录,例如在我的情况下是node_modules目录。 COPY [all but **/node_modules/**] .
使用Docker可以实现这个吗?
.dockerignore
文件(因此在这种情况下,很可能是一个父目录,包含node_modules),其中包含一行:**/node_modules
虽然您可能只是想要:
node_modules
关于dockerignore的信息:https://docs.docker.com/engine/reference/builder/#dockerignore-file
node_modules
是无效的。 - rustyx.dockerignore
文档中第一段说:“这有助于避免向守护程序发送不必要的大型或敏感文件和目录,并可能使用ADD或COPY将它们添加到映像中。”? - ceejayoz对于那些无法使用.dockerignore文件的人(例如,如果您需要在一个COPY中使用该文件但在另一个COPY中不需要):
是的,但您需要多个COPY指令。具体来说,您需要为要排除的文件名中的每个字母都需要一个COPY。
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'
COPY failed: no source files were specified
错误提示,但如果有以'nod'开头的文件则不会。 - mhecksrc/
复制到/var/www
,并将/conf/nginx.conf
复制到/etc/nginx/conf.d/default.conf
。此外,您可以复制所有内容,稍后再运行RUN rm -r conf
(以保持源树不变)。如果有其他建议,我很乐意听取。 - YuvalCOPY [^node_modules]*
就可以复制所有顶层文件。 - smdufb一行代码解决方案:在项目根目录下的命令提示符或终端中输入以下命令:
echo node_modules >> .dockerignore
此命令将在.dockerignore文件中追加“node_modules”。如果.dockerignore文件不存在,它将创建一个新文件。请将node_modules替换为您要排除的文件夹。
警告:如果您是 Docker 生态系统的新手和/或已经在项目中有.dockerignore文件,请在执行操作之前备份。
奖励:(由 Joey Baruch 指出)
(To CREATE/OVERWRITE the .dockerignore file via PowerShell, which can be handled by Docker):
>> echo node_modules | Out-File -Encoding UTF8 .dockerignore
从当前目录中排除 node_modules
node_modules
*/node_modules
**/node_modules
来同时执行这两个操作。 - bfontaine我采用了多阶段构建方法,因为我需要一个阶段能够访问该文件,而另一个阶段不能,所以 .dockerignore 无法解决这个问题:
FROM ruby AS builder
COPY app/ app/
# Do stuff with app
# remove the stuff you don't want
RUN rm -Rf app/assets
FROM ruby AS publish
# In my real version I needed the absolute path to builder WORKDIR.
# Since I'm copying from the builder stage, app/assets won't exist
# and neither will it be part of the publish image.
COPY --from=builder app app
对于使用gcloud build的用户:
gcloud build
会忽略.dockerignore
,而寻找.gcloudignore
。
请使用:
cp .dockerignore .gcloudignore
对于我来说,添加 .dockerignore 文件是有效的。
还有一个额外的提示,如果你在 Windows 上尝试此解决方案,Windows 不会让你创建 .dockerignore 文件(因为默认情况下不允许创建以.开头的文件)。
要在 Windows 上创建以 . 开头的文件,也包括结尾的句点,比如:.dockerignore.
并按回车键(前提是你已经从文件夹选项中启用了查看扩展名选项)。
.dockerignore
文件怎么样? - anthony sottile