gpt4 book ai didi

docker - 从命令行指定 dockerignore

转载 作者:行者123 更新时间:2023-12-01 00:18:35 34 4
gpt4 key购买 nike

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

docker build --ignore="node_modules" -t foo . 

有没有办法从命令行做到这一点?我在文档中没有看到这个: https://docs.docker.com/engine/reference/commandline/build/#build-with--

最佳答案

不, docker build 不提供 .dockerignore file 的替代方案.

这就是为什么我通常保留一个符号链接(symbolic link) .dockerignore指向实际.dockerignore_official ,除了某些情况,我将符号链接(symbolic link)切换到 .dockerignore_module .

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

2019 年 4 月更新:如 Alba Mendez 所述在 the comments , PR 901应该有帮助:

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 commit b9db1d2 .

它在 Docker v19.03.0 beta1 , 和 阿尔巴 已发布 example here :

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õnis Tiigi 的以下评论:

  • #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.

关于docker - 从命令行指定 dockerignore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50547489/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com