简体   繁体   中英

Mount Docker volume as non-persistent storage

For integration tests, I created a Docker volume including a database.

  • Use case 1: I want to use the same database over and over again starting at the exactly same state, and I also want to share it with other developers.
  • Use case 2: Sometimes, I want to modify the volume to include data for additional tests.

When I mount the volume, all write operations on the database will modify the volume, which is what I want to avoid. I know that it is possible to mount the volume as read-only, however this does also not work, because then the database is also read-only during the tests.

What I want to achieve is to mount the volume with write access, but without persisting its state after the container is closed. Just as if the volume data was included in the image itself.

I thought about doing just that - including the database in the image itself - but in this case my second use case (modifying the database from time to time to support additional tests) seems to be much more complicated (exporting the database from the container, modifying it outside of docker, and then creating a new image with that data).

Can anybody tell my the recommended way to solve that problem?

You can use tmpfs mounts.

docker run -d \
  -it \
  --name tmptest \
  --tmpfs /app \
  nginx:latest

As opposed to volumes and bind mounts, a tmpfs mount is temporary, and only persisted in the host memory. When the container stops, the tmpfs mount is removed, and files written there won't be persisted.

More on it here

Note: it works on Linux hosts only.

Looks like a use case for overlayfs; where each container gets its own writable layer while leaving the base intact. See more from this stackoverflow thread .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM