在Dockerfile中,我有

COPY . .

我想排除整个目录,在我的例子中是node_modules目录。

就像这样:

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

Docker能做到这一点吗?


当前回答

我使用了多阶段构建方法,因为我需要一个阶段来访问文件,但不需要另一个阶段,所以.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构建的用户:

Gcloud构建会忽略.dockerignore,而是寻找.gcloudignore

Use:

cp .dockerignore .gcloudignore

我使用了多阶段构建方法,因为我需要一个阶段来访问文件,但不需要另一个阶段,所以.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

在docker构建上下文目录中创建文件.dockerignore(在这种情况下,很可能是node_modules的父目录),其中包含一行:

**/node_modules

尽管你可能只是想:

node_modules

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

从当前目录中排除node_modules

node_modules

在任何直接子目录中不包含node_modules

*/node_modules

这是官方文件

添加.dockerignore对我有用。 那些在Windows上尝试此解决方案的人,Windows将不允许您创建.dockerignore文件(因为默认情况下不允许创建以。dockerignore开头的文件)。

从…开始创建这样的文件。在Windows上,还要包含一个结尾点,比如:.dockerignore。然后按回车键(前提是您已经从文件夹选项中启用了视图扩展选项)