Docker Community Forums

Share and learn in the Docker community.

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

I could reproduce the issue by running this on a MacOS system:

First open a shell in a container that already has docker-compose installed:

docker run --rm -v $(pwd):/work -v /var/run/docker.sock:/var/run/docker.sock --workdir /work -ti tmaier/docker-compose sh
I map the current folder so that I can work with my current project as if it were on my host.

And then inside the container:

docker-compose run testing-wiremock ls -lart
Now change the docker-compose.yml to the following:

version: ‘2.3’
services:
testing-wiremock:
image: rodolpheche/wiremock
volumes:
- /tmp:/home/wiremock/
and run again:

docker-compose run testing-wiremock ls -lart
This will show you the contents of the /tmp directory on the host (where the docker socket actually runs). To test you can even create a folder and a file in the /tmp and run the “docker-compose run” again. You will see the new files.

Moral of the story: If the mounted folder corresponds to an existing folder on the host where the docker daemon is actually running, then the mapping will actually work.

host -> container -> container (mounts here refer to paths on the host)

In your specific case the folder is mounted empty because the mounted path (check it by running docker-compose config) is not present on the host (host = the host running your Jenkins container, not the Jenkins container itself).