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 container with
docker run --volumes-from c0dbc34fd631 -d app_name/postgres
IMHO, that has the drawback, that I must not ever (by accident) delete container "c0dbc34fd631".
Another idea would be to mount host volumes "-v" into the container, however, the userid within the container does not necessarily match the userid from the host, and then permissions might be messed up.
Note: Instead of --volumes-from 'cryptic_id'
you can also use --volumes-from my-data-container
where my-data-container
is a name you assigned to a data-only container, e.g. docker run --name my-data-container ...
(see the accepted answer)
While this is still a part of Docker that needs some work, you should put the volume in the Dockerfile with the VOLUME instruction so you don't need to copy the volumes from another container.
That will make your containers less inter-dependent and you don't have to worry about the deletion of one container affecting another.
In Docker release v1.0, binding a mount of a file or directory on the host machine can be done by the given command:
The above volume could be used as a persistent storage on the host running Docker.
Use Persistent Volume Claim (PVC) from Kubernetes, which is a Docker container management and scheduling tool:
Persistent Volumes
The advantages of using Kubernetes for this purpose are that:
If you want to move your volumes around you should also look at Flocker.
From the README:
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.
Docker 1.9.0 and above
Use volume API
This means that the data-only container pattern must be abandoned in favour of the new volumes.
Actually the volume API is only a better way to achieve what was the data-container pattern.
If you create a container with a
-v volume_name:/container/fs/path
Docker will automatically create a named volume for you that can:docker volume ls
docker volume inspect volume_name
--volumes-from
connectionThe new volume API adds a useful command that lets you identify dangling volumes:
And then remove it through its name:
As @mpugach underlines in the comments, you can get rid of all the dangling volumes with a nice one-liner:
Docker 1.8.x and below
The approach that seems to work best for production is to use a data only container.
The data only container is run on a barebones image and actually does nothing except exposing a data volume.
Then you can run any other container to have access to the data container volumes:
In this blog post there is a good description of the so-called container as volume pattern which clarifies the main point of having data only containers.
Docker documentation has now the DEFINITIVE description of the container as volume/s pattern.
Following is the backup/restore procedure for Docker 1.8.x and below.
BACKUP:
RESTORE:
Here is a nice article from the excellent Brian Goff explaining why it is good to use the same image for a container and a data container.