I'm running a Java application that reads some data in a given format and writes a CSV file in the temp directory. After this file is fully written, the data is bulk loaded in the database. When the load is done, the file is immediately deleted. This file should not be shared.
Now, I want to run a jar of this application in a docker container.
From the docs, I've read:
Writing into a container’s writable layer requires a storage driver to manage the filesystem. The storage driver provides a union filesystem, using the Linux kernel. This extra abstraction reduces performance as compared to using data volumes, which write directly to the host filesystem.
From what I understand, it would be better if I create a volume to map the host /tmp folder so I can have better performance for I/O operations. On the other hand, it seems that volumes are used for persistence reasons, although my use case consists of temporary files.
Are volumes recomended when managing temporary files? If not, is there a efficient approach to deal with temporary files in docker?
By default, Docker will use /var/lib/docker/tmp for it's temporary directory.
To clean this up, you can use the docker container prune command. By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag. Other filtering expressions are available.
If you're running Docker on Linux, you have a third option: tmpfs mounts. When you create a container with a tmpfs mount, the container can create files outside the container's writable layer. As opposed to volumes and bind mounts, a tmpfs mount is temporary, and only persisted in the host memory.
You can create a tmpfs volume if you want performance and not persistence. E.g.
docker run --tmpfs /tmp -d java-img
Note that there is no persistence, even between container restarts. Also this will start with an empty directory rather than initializing from the image directory at that mount point.
For more details, including other ways to mount a tmpfs volume, see: https://docs.docker.com/storage/tmpfs/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With