dockerdocker-composedockerfiletmpfs

How to create tmpfs folder with Docker via volume instruction?


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?


Solution

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