如何通过Docker使用volume指令创建tmpfs文件夹?

huangapple go评论59阅读模式
英文:

How to create tmpfs folder with Docker via volume instruction?

问题

我想通过Dockerfile中的Docker指令创建tmps卷。这是否可行?是否有其他参数可以传递以配置卷指令?

英文:

I want to create tmps volume via docker instructions in dockerfile. Is it possible to do so? Are there additional parameters which I can pass to configure volume instruction?

答案1

得分: 0

以下是关于如何为 tmpfs 卷进行仪表化的官方文档,以及其中的一个简单示例:

docker run -d \
  -it \
  --name tmptest \
  --mount type=tmpfs,destination=/app \
  nginx:latest

相反,这个链接告诉您如何通过Docker Compose执行相同的操作:

 type: tmpfs
  target: /app
  tmpfs:
    size: 1000
英文:

Here is the Official documentation about how to instrument a tmpfs volume, and here a simple example from there:

docker run -d \
  -it \
  --name tmptest \
  --mount type=tmpfs,destination=/app \
  nginx:latest

Instead this link tells you how to do the same thing by Docker Compose:

 type: tmpfs
  target: /app
  tmpfs:
    size: 1000

答案2

得分: 0

在Dockerfile中,你几乎无法指定有关卷或其他类型的运行时配置的几乎任何信息。特别是没有办法指示容器应始终运行并挂载tmpfs。

有关运行时卷的唯一真正的Dockerfile语句是VOLUME指令。它没有配置,只有一个必须在其上挂载某种卷的目录列表。它的直接效果是在该目录上挂载一个匿名卷,如果没有其他内容的话;其间接效果是防止将来的RUN指令修改该目录。你几乎不需要它。

@AntonioPetricca的答案显示了一个docker run --mount type=tmpfs选项,如果你想要将tmpfs容器挂载到另一个容器中,那么在创建它时 必须 指定这个选项(或等效的Compose)。目标目录不需要是VOLUME目录。在Dockerfile中没有强制此选项的方法。

在更有限的范围内,你可以使用RUN --mount=type=tmpfs,target=/directory来运行单个Dockerfile构建步骤。再次强调,命名目录不会在镜像中持久存在。挂载不会超出单个RUN命令的范围。

英文:

In a Dockerfile, you can specify almost nothing about volumes or several other types of runtime configuration. In particular there is no way to indicate a container should always run with a tmpfs mounted.

The only real Dockerfile statement about volumes at runtime is the VOLUME directive. It has no configuration, only a list of directories that must have some sort of volume mounted on them. Its direct effect is to cause an anonymous volume to be mounted on that directory if nothing else is; its indirect effect is to prevent future RUN directives from modifying the directory. You rarely if ever need it.

@AntonioPetricca's answer shows a docker run --mount type=tmpfs option, and if you want to mount a tmpfs container into an container, you must specify this (or a Compose equivalent) when you create it. The target directory does not need to be a VOLUME directory. There is no way to force this option in the Dockerfile.

In a more limited scope, you can run an individual Dockerfile build step with RUN --mount=type=tmpfs,target=/directory. Again, the named directory won't be persisted in the image. The mount won't last beyond that single RUN command though.

huangapple
  • 本文由 发表于 2023年7月10日 17:30:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定