Docker run empties tmpfs volumes after run

Hi all,

This is my first question, hope this meets the etiquette. My problem is as follows: when I create a volume, the volume is emptied (though not removed) as soon as the last container using it shuts down. For instance:

$ docker volume create \
  --driver local \
  --opt type=tmpfs \
  --opt device=tmpfs \
  --opt o=size=10G \
  test
$ docker run --rm -ti -v test:/test /bin/bash # here I can write to the volume
$ # in another terminal
$ docker run --rm -ti -v test:/test /bin/bash # here I can see the same files as in the first volume
$ # now I Ctrl+C both commands above
$ docker run -ti -v test:/test /bin/bash # /test is empty again, all changes made above have disappeared

The volume is still present in docker volume ls, but if I inspect it and ls its mount point on the host, it is empty. Why does the volume get emptied when the last container shuts down, and what can I do to prevent that?

A volume is more like a handle than a storage: it holds all required information to mount a ressource when a consuming container is running and unmounted when it stops. Since you choose to use a volume baked by tmpfs, the tmpfs will be released when the last consuming container is stopped and newly created when a new conainer using it is created.

Looks like “works as configured” to me.

2 Likes