问题描述
我有一个 .dockerignore 文件,但是对于一个用例,我想在命令行中指定 .dockerignore 的内容,例如:
docker build --ignore="node_modules" -t foo .
有没有办法从命令行执行此操作?我在文档中没有看到这个:https://docs.docker.com/engine/reference/commandline/build/#build-with--
不,docker build
不提供 .dockerignore
文件.
这就是为什么我通常保留一个指向实际 .dockerignore_official
的符号链接 .dockerignore
,除了某些情况,我将符号链接切换到 .dockerignore_module
.
这是一种解决方法,但它允许我对我可能需要的不同版本的 dockerignore 进行版本控制,并在两者之间进行选择.
2019 年 4 月更新:正如 Alba Mendez 在 评论, PR 901 应该会有所帮助:
dockerfile:添加 dockerignore 覆盖支持
前端将首先检查 .dockerignore
,如果找到,将使用它来代替根 .dockerignore
.
请参阅 moby/buildkit
提交 b9db1d2.p>
它位于 Docker v19.03.0 beta1,并且 Alba 已在 此处发布示例:
您需要启用 Buildkit 模式才能使用 i
$ export DOCKER_BUILDKIT=1$ echo "FROM ubuntu
COPY .tmp/" >Dockerfile$ cp Dockerfile Dockerfile2$ touch foo bar$回声富">.dockerignore$回声酒吧">Dockerfile2.dockerignore$ docker build -t container1 -f Dockerfile .$ docker build -t container2 -f Dockerfile2 .$ docker run container1 ls tmpDockerfileDockerfile2Dockerfile2.dockerignore酒吧$ docker run container2 ls tmpDockerfileDockerfile2Dockerfile2.dockerignore富
2019 年 8 月更新:现在在 Docker 19.03 中,来自 Tõnis Tiigi 的以下评论:
- #12886 (comment) 允许为每个 Dockerfile 设置一个 dockerignore 文件如果存储库包含很多.(请注意,这是对最初问题的准确描述)
- BuildKit 会自动忽略不使用的文件,自动消除不同组文件需要相互忽略的问题.
- 同一个 Dockerfile 对不同的构建模式"(例如 dev 与 prod)使用不同的文件集的情况可以通过多阶段构建并使用构建参数定义模式更改来实现.
I have a .dockerignore file, but for one use case, I want to specify the contents of .dockerignore at the command line, something like:
docker build --ignore="node_modules" -t foo .
is there a way to do this from the command line? I am not seeing this in the docs: https://docs.docker.com/engine/reference/commandline/build/#build-with--
No, docker build
does not offer an alternative to the .dockerignore
file.
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
.
This is a workaround, but that allows me to version the different version of dockerignore I might need, and choose between the two.
Update April 2019: as mentioned by Alba Mendez in the comments, PR 901 should help:
See moby/buildkit
commit b9db1d2.
It is in Docker v19.03.0 beta1, and Alba has posted an example here:
$ export DOCKER_BUILDKIT=1
$ echo "FROM ubuntu
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
Update August 2019: this is now in Docker 19.03, with the following comment from Tõnis Tiigi:
这篇关于从命令行指定 dockerignore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!