Docker ADD与VOLUME [英] Docker ADD vs VOLUME
问题描述
我正在学习Docker,并且对于何时何地使用 ADD
和 VOLUME
有疑问。我认为这两种方法都可以做到:
I am learning Docker and I have doubts about when and where to use ADD
and VOLUME
. Here is what I think both of these do:
在构建时将文件复制到映像中。该映像包含所有文件,因此您可以非常轻松地进行部署。另一方面,在每次开发中都需要构建似乎不是一个好主意,因为构建需要开发人员运行命令来重建容器。另外,建造容器可能很耗时。
Copy files to the image at build time. The image has all the files so you can deploy very easily. On the other hand, needing to build every time doesn't look like a good idea in development because building requires the developer to run a command to rebuild the container; additionally, building the container can be time-consuming.
我知道使用 docker run -v
,您可以在容器内安装主机文件夹,这样您就可以轻松地修改文件,并观察容器中的应用对更改的反应。在开发中看起来不错,但我不确定如何以这种方式部署文件。
I understand that using docker run -v
you can mount a host folder inside your container, this way you can easily modify files and watch the app in your container react to the changes. It looks great in development, but I am not sure how to deploy my files this way.
推荐答案
ADD
这两者之间的根本区别在于, ADD
可以实际添加您要添加的内容,无论是文件夹还是只是文件部分图片。使用您之后制作的图片的任何人都可以访问您 ADD
中的任何内容。即使后来删除它,这也是正确的,因为Docker工作在多层中,并且 ADD
层仍将作为映像的一部分存在。需要明确的是,您在构建时仅 ADD
,而在运行时则从未 ADD
。
ADD
The fundamental difference between these two is that ADD
makes whatever you're adding, be it a folder or just a file actually part of your image. Anyone who uses the image you've built afterwards will have access to whatever you ADD
. This is true even if you afterwards remove it because Docker works in layers and the ADD
layer will still exist as part of the image. To be clear, you only ADD
something at build time and cannot ever ADD
at run-time.
一些您想使用 ADD
的例子:
- 在您要引用并安装在Dockerfile中的requirements.txt文件中有一些要求。然后,您可以执行以下操作:
ADD ./requirements.txt /requirements.txt
然后是RUN pip install -r /requirements.txt$c$ c>
-
您想将应用程序代码用作Dockerfile中的上下文,例如,如果您希望将应用程序目录设置为映像中的工作目录,从映像运行的容器中的默认命令实际运行您的应用程序,您可以执行以下操作:
- You have some requirements in a requirements.txt file that you want to reference and install in your Dockerfile. You can then do:
ADD ./requirements.txt /requirements.txt
followed byRUN pip install -r /requirements.txt
You want to use your app code as context in your Dockerfile, for example, if you want to set your app directory as the working dir in your image and to have the default command in a container run from your image actually run your app, you can do:
ADD ./ / usr / local / git / my_app
WORKDIR / usr / local / git / my_app
CMD python ./main.py
卷仅允许从图像运行的容器可以访问该容器所在的任何本地计算机上的某个路径正在运行。您无法使用Dockerfile中的 VOLUME
目录中的文件。卷目录中的任何内容将在构建时不可访问,但将在运行时访问。
Volume, on the other hand, just lets a container run from your image have access to some path on whatever local machine the container is being run on. You cannot use files from your VOLUME
directory in your Dockerfile. Anything in your volume directory will not be accessible at build-time but will be accessible at run-time.
一些您想使用 VOLUME
的例子:
- 在您的容器中运行的应用会在
/ var / log / my_app
中创建日志。您希望这些日志在主机上可以访问,而在删除容器时不要删除。您可以通过添加VOLUME / var / log / my_app
/ var / log / my_app 上创建挂载点来实现>到Dockerfile,然后使用docker运行容器,运行-v / host / log / dir / my_app:/ var / log / my_app some_repo / some_image:some_tag
- 您有一些本地设置文件,希望容器中的应用可以访问。也许这些设置文件在本地计算机,开发环境和生产环境上是不同的。特别是如果这些设置文件是机密的,则在这种情况下,您肯定不希望它们出现在图像中。在这种情况下,一个好的策略是将
VOLUME / etc / settings / my_app_settings
添加到您的Dockerfile中,并使用docker run -v / host / settings / dir:/ etc / settings / my_app_settings some_repo / some_image:some_tag
,并确保/ host / settings / dir存在于您希望应用程序运行的所有环境中。
- The app being run in your container makes logs in
/var/log/my_app
. You want those logs to be accessible on the host machine and not to be deleted when the container is removed. You can do this by creating a mount point at/var/log/my_app
by addingVOLUME /var/log/my_app
to your Dockerfile and then running your container withdocker run -v /host/log/dir/my_app:/var/log/my_app some_repo/some_image:some_tag
- You have some local settings files you want the app in the container to have access to. Perhaps those settings files are different on your local machine vs dev vs production. Especially so if those settings files are secret, in which case you definitely do not want them in your image. A good strategy in that case is to add
VOLUME /etc/settings/my_app_settings
to your Dockerfile, run your container withdocker run -v /host/settings/dir:/etc/settings/my_app_settings some_repo/some_image:some_tag
, and make sure the /host/settings/dir exists in all environments you expect your app to be run.
这篇关于Docker ADD与VOLUME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!