I have been working on this project recently in which a couple of docker containers are built along the way and they end up being sent to different third-party servers. Due to privacy reasons, some specific files must not be sent to particular servers. Hence, each container has its own blacklist of files it should not accept inside. This should be handled by the .dockerignore files, except that my .dockerignores got, well, ignored (no pun intended).
我最近一直在从事这个项目,在该项目中,沿途构建了两个Docker容器,它们最终被发送到不同的第三方服务器。 由于隐私原因,某些特定文件不能发送到特定服务器。 因此,每个容器都有其自己不应在内部接受的文件黑名单。 这应该由.dockerignore文件处理,除了我的.dockerignores被很好地忽略(没有双关语)。
It took me hours to find the solution, which, obviously, turned out to be a one-liner.
我花了几个小时才找到解决方案,很显然,这是一成不变的。
I hope I can save you some miserable time by suggesting you set the environmental variable DOCKER_BUILDKIT to 1 straight away.
希望建议您立即将环境变量DOCKER_BUILDKIT设置为1,以节省一些时间。
That’s it, that’s the trick. If you wish, feel free to read on for some background.
就是这样,这就是窍门。 如果您愿意,请随时阅读一些背景知识。
According to the Docker documentation, when the container is being built, the Docker client looks for a .dockerignore
file matching the Dockerfile’s name. Hence, if you have a Dockerfile called my_app_dockerfile
, Docker looks for my_app_dockerfile.dockerignore
. If it doesn’t exist, then it looks for simply .dockerignore
. Unfortunately, this did not work for me. I had two Dockerfiles with the matching .dockerignores, but none of the latter was detected.
按照多克尔文档,当容器被建成,多克尔客户端查找.dockerignore
文件匹配Dockerfile的名字。 因此,如果您有一个名为my_app_dockerfile
,则Docker将寻找my_app_dockerfile.dockerignore
。 如果不存在,那么它只会.dockerignore
。 不幸的是,这对我没有用。 我有两个带有匹配的.dockerignores的Dockerfile,但是没有一个被检测到。
Browsing the web for the answer has finally led me to this github issue comment explaining that .dockerignores indeed work as described in the docs as long as the DOCKER_BUILDKIT
environmental variable is set to 1. But what is it?
浏览网页终于找到了这个github问题评论,解释了.dockerignores确实按照文档中的描述工作,只要DOCKER_BUILDKIT
环境变量设置为1。但这是什么?
Docker BuildKit is a relatively new feature, fully released in mid-2019 with Docker 19.03. Its aim is to increase the performance of container-building by reading and building Dockerfile lines in parallel, among others. It turns out it’s also needed for .dockerignore-detection to work properly.
Docker BuildKit是一个相对较新的功能,已在Docker 19.03中于2019年中全面发布。 其目的是通过并行读取和构建Dockerfile行来提高容器构建的性能。 事实证明,.dockerignore-detection正常工作也需要。
On a MacOS, you can enable BuildKit permanently from Docker Desktop by going to preferences — deamon — advanced and adding the features
field to the json with buildkit
set to true
:
在MacOS上,您可以通过以下方式从Docker桌面永久启用BuildKit:转到首选项-deamon-高级,并将features
字段添加到json, buildkit
设置为true
:
Thanks for reading! I hope you will find this piece of advice useful. If you liked this post, try one of my other articles. Can’t choose? Pick one of these:
谢谢阅读! 我希望您会发现此建议有用。 如果您喜欢这篇文章,请尝试我的其他文章之一。 无法选择? 选择以下之一: