如何在Docker的构建上下文之外包含文件?

本文翻译自:How to include files outside of Docker's build context?

How can I include files from outside of Docker's build context using the "ADD" command in the Docker file? 如何使用Docker文件中的“ ADD”命令从Docker构建上下文之外包含文件?

From the Docker documentation: 从Docker文档中:

The path must be inside the context of the build; 路径必须在构建上下文内; you cannot ADD ../something/something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon. 您不能添加../something/something,因为Docker构建的第一步是将上下文目录(和子目录)发送到docker守护程序。

I do not want to restructure my whole project just to accommodate Docker in this matter. 我不想为了适应Docker而重组我的整个项目。 I want to keep all my Docker files in the same sub-directory. 我想将所有Docker文件保留在同一子目录中。

Also, it appears Docker does not yet (and may not ever) support symlinks: Dockerfile ADD command does not follow symlinks on host #1676. 此外,似乎Docker尚未(可能从未)支持符号链接: Dockerfile ADD命令未遵循主机#1676上的符号链接。

The only other thing I can think of is to include a pre-build step to copy the files into the Docker build context (and configure my version control to ignore those files). 我唯一能想到的就是包括一个预构建步骤,将文件复制到Docker构建上下文中(并配置我的版本控制以忽略这些文件)。 Is there a better workaround for than that? 有没有比这更好的解决方法了?


#1楼

参考:https://stackoom.com/question/1pZmG/如何在Docker的构建上下文之外包含文件


#2楼

If you read the discussion in the issue 2745 not only docker may never support symlinks they may never support adding files outside your context. 如果您阅读了《 问题2745》中的讨论,不仅docker可能永远都不支持符号链接,他们也可能永远都不支持在上下文之外添加文件。 Seems to be a design philosophy that files that go into docker build should explicitly be part of its context or be from a URL where it is presumably deployed too with a fixed version so that the build is repeatable with well known URLs or files shipped with the docker container. 似乎是一种设计理念,进入docker build的文件应该明确地是其上下文的一部分,或者也应该来自URL(假定它也以固定版本进行部署),从而使该构建可以与众所周知的URL或随Docker一起提供的文件重复进行。泊坞窗容器。

I prefer to build from a version controlled source - ie docker build -t stuff http://my.git.org/repo - otherwise I'm building from some random place with random files. 我更喜欢从受版本控制的源进行构建-即docker build -t stuff http://my.git.org/repo-否则,我将从某个具有随机文件的随机位置进行构建。

fundamentally, no.... -- SvenDowideit, Docker Inc 从根本上说...-Docker Inc. SvenDowideit

Just my opinion but I think you should restructure to separate out the code and docker repositories. 只是我的意见,但我认为您应该进行重组以分离出代码和docker存储库。 That way the containers can be generic and pull in any version of the code at run time rather than build time. 这样,容器可以是通用的,并且可以在运行时而不是构建时引入任何版本的代码。

Alternatively, use docker as your fundamental code deployment artifact and then you put the dockerfile in the root of the code repository. 或者,将docker用作基本代码部署工件,然后将dockerfile放入代码存储库的根目录中。 if you go this route probably makes sense to have a parent docker container for more general system level details and a child container for setup specific to your code. 如果您采用这种方法,那么有一个父Docker容器(用于更一般的系统级详细信息)和一个子容器(用于特定于您的代码的设置)可能是有意义的。


#3楼

On Linux you can mount other directories instead of symlinking them 在Linux上,您可以挂载其他目录,而不用符号链接它们

mount --bind olddir newdir

See https://superuser.com/questions/842642 for more details. 有关更多详细信息,请参见https://superuser.com/questions/842642

I don't know if something similar is available for other OSes. 我不知道其他操作系统是否可以使用类似的软件。 I also tried using Samba to share a folder and remount it into the Docker context which worked as well. 我还尝试使用Samba共享一个文件夹并将其重新安装到同样有效的Docker上下文中。


#4楼

The best way to work around this is to specify the Dockerfile independently of the build context, using -f. 解决此问题的最佳方法是使用-f独立于构建上下文指定Dockerfile。

For instance, this command will give the ADD command access to anything in your current directory. 例如,此命令将使ADD命令可以访问您当前目录中的任何内容。

docker build -f docker-files/Dockerfile .

Update : Docker now allows having the Dockerfile outside the build context (fixed in 18.03.0-ce, https://github.com/docker/cli/pull/886 ). 更新 :Docker现在允许将Dockerfile置于构建上下文之外(已在18.03.0-ce, https: //github.com/docker/cli/pull/886中修复)。 So you can also do something like 所以你也可以做类似的事情

docker build -f ../Dockerfile .

#5楼

You can also create a tarball of what the image needs first and use that as your context. 您还可以创建一个首先需要图像的压缩包,然后将其用作上下文。

https://docs.docker.com/engine/reference/commandline/build/#/tarball-contexts https://docs.docker.com/engine/reference/commandline/build/#/tarball-contexts


#6楼

I believe the simpler workaround would be to change the 'context' itself. 我认为更简单的解决方法是更改​​“上下文”本身。

So, for example, instead of giving: 因此,例如,与其给出:

docker build -t hello-demo-app .

which sets the current directory as the context, let's say you wanted the parent directory as the context, just use: 它将当前目录设置为上下文,假设您希望将父目录作为上下文,只需使用:

docker build -t hello-demo-app ..
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值