且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从命令行指定dockerignore

更新时间:2023-02-22 09:26:57

不, docker build 不能替代 .dockerignore 文件

No, docker build does not offer an alternative to the .dockerignore file.

这就是为什么我通常保持符号链接 .dockerignore 指向实际的原因。 dockerignore_official ,但在某些情况下,我将符号链接切换为 .dockerignore_module

That is why I usually keep a symbolic link .dockerignore pointing to the actual .dockerignore_official, except for certain case, where I switch the symlink to .dockerignore_module.

这是一种解决方法,但这使我可以对可能需要的不同版本的dockerignore进行版本控制,然后在两者之间进行选择。

This is a workaround, but that allows me to version the different version of dockerignore I might need, and choose between the two.

2019年4月更新:如 Alba Mendez所述评论中的a> PR 901 应该会帮助:

Update April 2019: as mentioned by Alba Mendez in the comments, PR 901 should help:


dockerfile:添加dockerignore覆盖支持



前端将首先检查< path / to / Dockerfile> .dockerignore ,如果找到它,它将代替根 .dockerignore

dockerfile: add dockerignore override support

Frontend will first check for <path/to/Dockerfile>.dockerignore and, if it is found, it will be used instead of the root .dockerignore.

请参见 moby / buildkit 提交b9db1d2

See moby/buildkit commit b9db1d2.

它在 Docker v19.03.0 beta1 ,并且 Alba 发布了此处的示例


您需要启用Buildkit模式才能使用i

You need to enable Buildkit mode to use i



$ export DOCKER_BUILDKIT=1

$ echo "FROM ubuntu \n COPY . tmp/" > Dockerfile
$ cp Dockerfile Dockerfile2
$ touch foo bar
$ echo "foo" > .dockerignore
$ echo "bar" > Dockerfile2.dockerignore

$ docker build -t container1 -f Dockerfile .
$ docker build -t container2 -f Dockerfile2 .
$ docker run container1 ls tmp
Dockerfile
Dockerfile2
Dockerfile2.dockerignore
bar

$ docker run container2 ls tmp
Dockerfile
Dockerfile2
Dockerfile2.dockerignore
foo

2019年8月更新:此版本现在位于Docker 19.03中,以下注释来自天尼斯·蒂吉(TõnisTiigi)

Update August 2019: this is now in Docker 19.03, with the following comment from Tõnis Tiigi:



  • #12886(评论)允许为每个Dockerfile设置dockerignore文件(如果存储库包含很多)(请注意,这是对initia的确切描述l问题)

  • BuildKit自动忽略未使用的文件,自动解决了不同文件集需要彼此忽略的问题。

  • 相同Dockerfile对不同的模式使用不同的文件集的情况(例如dev vs prod)可以通过多阶段构建并通过构建参数定义模式更改来实现。

  • #12886 (comment) allows setting a dockerignore file per Dockerfile if the repository contains many. (Note that this was the exact description for the initial issue)
  • BuildKit automatically ignores files that are not used, automatically removing the problem where different sets of files needed to ignore each other.
  • The cases where same Dockerfile uses different sets of files for different "modes" of build (eg. dev vs prod) can be achieved with multi-stage builds and defining the mode changes with build arguments.