How do people deal with persistent storage for your Docker containers?
I am currently using this approach: build the image, e.g. for PostgreSQL, and then start the c
@tommasop's answer is good, and explains some of the mechanics of using data-only containers. But as someone who initially thought that data containers were silly when one could just bind mount a volume to the host (as suggested by several other answers), but now realizes that in fact data-only containers are pretty neat, I can suggest my own blog post on this topic: Why Docker Data Containers (Volumes!) are Good
See also: my answer to the question "What is the (best) way to manage permissions for Docker shared volumes?" for an example of how to use data containers to avoid problems like permissions and uid/gid mapping with the host.
To address one of the OP's original concerns: that the data container must not be deleted. Even if the data container is deleted, the data itself will not be lost as long as any container has a reference to that volume i.e. any container that mounted the volume via --volumes-from
. So unless all the related containers are stopped and deleted (one could consider this the equivalent of an accidental rm -fr /
) the data is safe. You can always recreate the data container by doing --volumes-from
any container that has a reference to that volume.
As always, make backups though!
UPDATE: Docker now has volumes that can be managed independently of containers, which further makes this easier to manage.
It depends on your scenario (this isn't really suitable for a production environment), but here is one way:
Creating a MySQL Docker Container
This gist of it is to use a directory on your host for data persistence.