Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage temporary files in docker?

Tags:

docker

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?

like image 987
d4nielfr4nco Avatar asked Oct 05 '18 03:10

d4nielfr4nco


People also ask

Where are docker tmp files stored?

By default, Docker will use /var/lib/docker/tmp for it's temporary directory.

How do I clean up unused docker files?

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.

Does docker have tmp?

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.


1 Answers

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/

like image 151
BMitch Avatar answered Sep 26 '22 13:09

BMitch