从命令行指定dockerignore [英] Specify dockerignore from command line

查看:100
本文介绍了从命令行指定dockerignore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.dockerignore文件,但是对于一个用例,我想在命令行中指定.dockerignore的内容,例如:

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 . 

是否可以从命令行执行此操作?我在文档中没有看到此内容: https:// docs .docker.com / engine / reference / commandline / build /#build-with--

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--

推荐答案

不, 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.

这篇关于从命令行指定dockerignore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆