Docker临时文件策略 [英] Docker temporary files strategy

查看:128
本文介绍了Docker临时文件策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的码头工人会产生一些临时文件.

My docker produces some temporary files.

是否有关于这些的鼓励策略?

Is there an encouraged strategy regarding those?

如果我将它们放在/tmp 中,请我不确定它们会被清除.

If I put those to /tmp, I'm not sure they'll get cleared.

还是应该从主机公开卷/tmp ?

Or should I expose the volume /tmp from the host machine?

谢谢

推荐答案

我不知道有任何鼓励使用Docker管理临时文件的方法,因为这主要取决于您如何在应用程序中处理这些临时文件(应该它们会在重新启动时被删除吗?是否定期?...)

I am not aware of any encouraged way to manage temporary files with Docker as it will mostly depend on how you need to handle these temporary files with your application (should they be deleted on restart? Periodically?...)

根据您的需要,您有几种可能:

You have several possibilities depending on your needs:

您可以挂载 tmpfs 卷,该卷将在容器运行时持久保存数据(即,当容器停止时,该卷中的数据将被删除),例如:

You can mount a tmpfs volume which will persist data as long as the container is running (i.e. the data in the volume will be deleted when the container stops), for example:

docker run --mount type=tmpfs,destination=/myapp/tmpdir someimage

如果您可以定期重新启动容器,并且在重新启动容器时可以重新创建临时数据,则这可能很有用.但是,如果您需要能够在容器运行时清理临时数据,则这不是一个好的解决方案,因为您将需要停止容器以清理临时数据.

This may be useful if you (can) restart your containers regularly and the temporary data may be recreated on container restart. However if you need to be able to clean up temporary data while the container is running, this is not a good solution as you will need to stop the container to have your temporary data cleaned.

根据@ alexander-azarov注释,默认情况下,tmpfs卷大小不受限制,存在容器耗尽所有机器内存的风险.建议使用 tmpfs-size 标志减轻这种风险,例如 docker run --mount type = tmpfs,destination =/app,tmpfs-size = 4096

如果未安装任何卷,则容器的可写层是将所有数据写入容器的位置.它在容器重启时将保持不变,但是如果删除了容器,它将被删除.

The writable layer of the container is where all the data will be written in the container if no volume is mounted. It will persist on container restart, but will be deleted if the container is deleted.

这样,仅当删除容器时,才会删除临时数据.对于短寿命的容器,这可能是一个很好的解决方案,但对于长寿命的容器却不是.

This way the temporary data will be deleted only when the container is deleted. It may be a good solution for short-lived containers, but not for long-lived containers.

例如:

docker run -v /tmp/myapp-tmp-dir:/myapp/tmpdir someimage

这将导致所有数据都写入主机/tmp/myapp-tmp-dir 目录中,结果取决于主机如何管理/tmp (在大多数情况下,计算机重新启动后会清除数据)

This will cause all data to be written in the host machine /tmp/myapp-tmp-dir directory, and result will depend on how the host machine manage /tmp (in most cases, data are cleared upon machine restart)

您可以创建一个包含数据的卷,例如:

You can create a volume which will contain your data, for example:

docker run --mount source=myappvol,target=/myapp/tmpdir someimage

并管理卷中的数据:将其安装在另一个容器中并清理数据,删除卷等.

And manage the data in the volume: mount-it in another container and cleanup the data, deleting the volume, etc.

这些是(几乎)完全依赖Docker功能的最常见解决方案.另一种可能性是直接从容器中运行的软件或应用程序处理临时文件,但这是与应用程序相关的问题,而不是与Docker相关的问题.

These are the most common solutions relying (almost) solely on Docker functionalities. Another possibility would be to handle temporary files directly from your software or app running in the container, but it's more an application-related issue than a Docker-related one.

这篇关于Docker临时文件策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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