Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker temporary files strategy

Tags:

docker

My docker produces some temporary files.

Is there an encouraged strategy regarding those?

If I put those to /tmp, I'm not sure they'll get cleared. (Edit note: the link is dead. The question was, "Are default cronjobs executed in a docker container?")

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

like image 704
Augustin Riedinger Avatar asked Mar 15 '17 16:03

Augustin Riedinger


1 Answers

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:

Use Docker tmpfs mount

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.

Edit: as per @alexander-azarov coment, the tmpfs volume size is unlimited by default with the risk of the container using up all the machine memory. Using tmpfs-size flag is recommended to mitigate that risk, such as docker run --mount type=tmpfs,destination=/app,tmpfs-size=4096

Writing into the container writable layer

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.

Mounting host machine /tmp in the container with a bind mount

For example:

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

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)

Create and mount a volume to manage data into

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.


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.

like image 126
Pierre B. Avatar answered Nov 01 '22 08:11

Pierre B.