Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Docker 1.9.0 and above</h2> <p>Use <a href="https://docs.docker.com/engine/reference/commandline/volume_create/" rel="nofollow noreferrer">volume API</a></p> <pre><code>docker volume create --name hello docker run -d -v hello:/container/path/for/volume container_image my_command </code></pre> <p>This means that the data-only container pattern must be abandoned in favour of the new volumes.</p> <p>Actually the volume API is only a better way to achieve what was the data-container pattern.</p> <p>If you create a container with a <code>-v volume_name:/container/fs/path</code> Docker will automatically create a named volume for you that can:</p> <ol> <li>Be listed through the <code>docker volume ls</code></li> <li>Be identified through the <code>docker volume inspect volume_name</code></li> <li>Backed up as a normal directory</li> <li>Backed up as before through a <code>--volumes-from</code> connection</li> </ol> <p>The new volume API adds a useful command that lets you identify dangling volumes:</p> <pre><code>docker volume ls -f dangling=true </code></pre> <p>And then remove it through its name:</p> <pre><code>docker volume rm &lt;volume name&gt; </code></pre> <p>As @mpugach underlines in the comments, you can get rid of all the dangling volumes with a nice one-liner:</p> <pre><code>docker volume rm $(docker volume ls -f dangling=true -q) # Or using 1.13.x docker volume prune </code></pre> <h2>Docker 1.8.x and below</h2> <p>The approach that seems to work best for production is to use a <strong>data only container</strong>.</p> <p>The data only container is run on a barebones image and actually does nothing except exposing a data volume.</p> <p>Then you can run any other container to have access to the data container volumes:</p> <pre><code>docker run --volumes-from data-container some-other-container command-to-execute </code></pre> <ul> <li><a href="http://www.offermann.us/2013/12/tiny-docker-pieces-loosely-joined.html" rel="nofollow noreferrer">Here</a> you can get a good picture of how to arrange the different containers.</li> <li><a href="http://crosbymichael.com/advanced-docker-volumes.html" rel="nofollow noreferrer">Here</a> there is a good insight on how volumes work.</li> </ul> <p>In <a href="http://container42.com/2013/12/16/persistent-volumes-with-docker-container-as-volume-pattern/" rel="nofollow noreferrer">this blog post</a> there is a good description of the so-called <strong>container as volume pattern</strong> which clarifies the main point of having <strong>data only containers</strong>.</p> <p><a href="https://docs.docker.com/engine/userguide/dockervolumes/" rel="nofollow noreferrer">Docker documentation has now the DEFINITIVE description of the <strong>container as volume/s</strong> pattern.</a></p> <p>Following is the backup/restore procedure for Docker 1.8.x and below.</p> <p><strong>BACKUP:</strong></p> <pre><code>sudo docker run --rm --volumes-from DATA -v $(pwd):/backup busybox tar cvf /backup/backup.tar /data </code></pre> <ul> <li>--rm: remove the container when it exits</li> <li>--volumes-from DATA: attach to the volumes shared by the DATA container</li> <li>-v $(pwd):/backup: bind mount the current directory into the container; to write the tar file to</li> <li>busybox: a small simpler image - good for quick maintenance</li> <li>tar cvf /backup/backup.tar /data: creates an uncompressed tar file of all the files in the /data directory</li> </ul> <p><strong>RESTORE:</strong></p> <pre><code># Create a new data container $ sudo docker run -v /data -name DATA2 busybox true # untar the backup files into the new container᾿s data volume $ sudo docker run --rm --volumes-from DATA2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar data/ data/sven.txt # Compare to the original container $ sudo docker run --rm --volumes-from DATA -v `pwd`:/backup busybox ls /data sven.txt </code></pre> <p>Here is a nice <a href="http://container42.com/2014/11/18/data-only-container-madness/" rel="nofollow noreferrer">article from the excellent Brian Goff</a> explaining why it is good to use the same image for a container and a data container.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload