docker - 如何为同一项目中的不同构建指定不同的 .dockerignore 文件?

标签 docker

我曾经在 .dockerignore 中列出 tests 目录,这样它就不会被包含在我用来运行 Web 服务的镜像中。

现在我正在尝试使用 Docker 来运行我的单元测试,在这种情况下,我希望包含 tests 目录。

我检查了 docker build -h 并没有发现相关的选项。

我该怎么做?

最佳答案

Docker 19.03 为此提供了解决方案。

The Docker client tries to load <dockerfile-name>.dockerignore first and then falls back to .dockerignore if it can't be found. So docker build -f Dockerfile.foo . first tries to load Dockerfile.foo.dockerignore.

设置DOCKER_BUILDKIT=1当前需要环境变量才能使用此功能。此标志可与 docker compose 一起使用自从 1.25.0-rc3also specifying COMPOSE_DOCKER_CLI_BUILD=1 .

另见 comment0 , comment1 , comment2


来自 Mugen comment ,请注意

the custom dockerignore should be in the same directory as the Dockerfile and not in root context directory like the original .dockerignore

打电话时

DOCKER_BUILDKIT=1
docker build -f /path/to/custom.Dockerfile ...

您的.dockerignore文件应该在

/path/to/custom.Dockerfile.dockerignore

关于docker - 如何为同一项目中的不同构建指定不同的 .dockerignore 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40904409/

相关文章:

docker - 使用 Docker Pipeline Plugin 时,在 Jenkins 中停止和删除 docker 容器的正确方法是什么?

postgresql - 如何在 Postgres Dockerfile 中正确设置 VOLUME 和 CMD 指令?

linux - 如何在 docker 容器内自动启动服务

docker - 检测 Docker 镜像是否会在运行构建时发生变化

amazon-web-services - Docker 私有(private)注册表 : ping attempt failed

selenium - 如何使Zalenium与AWS Fargate一起使用?

java - Quarkus:dockerfile.jvm 与 dockerfile.native

docker - 如何显示图像 docker 的 docker 文件

amazon-web-services - 如何在 EC2 容器中运行 Docker 镜像?

docker - 是否可以在同一主机上运行多个 nginx docker 容器?