github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/docs/sources/userguide/dockervolumes.md (about)

     1  page_title: Managing data in containers
     2  page_description: How to manage data inside your Docker containers.
     3  page_keywords: Examples, Usage, volume, docker, documentation, user guide, data, volumes
     4  
     5  # Managing data in containers
     6  
     7  So far we've been introduced to some [basic Docker
     8  concepts](/userguide/usingdocker/), seen how to work with [Docker
     9  images](/userguide/dockerimages/) as well as learned about [networking
    10  and links between containers](/userguide/dockerlinks/). In this section
    11  we're going to discuss how you can manage data inside and between your
    12  Docker containers.
    13  
    14  We're going to look at the two primary ways you can manage data in
    15  Docker.
    16  
    17  * Data volumes, and
    18  * Data volume containers.
    19  
    20  ## Data volumes
    21  
    22  A *data volume* is a specially-designated directory within one or more
    23  containers that bypasses the [*Union File
    24  System*](/terms/layer/#union-file-system). Data volumes provide several 
    25  useful features for persistent or shared data:
    26  
    27  - Volumes are initialized when a container is created. If the container's
    28    base image contains data at the specified mount point, that existing data is 
    29    copied into the new volume upon volume initialization.
    30  - Data volumes can be shared and reused among containers.
    31  - Changes to a data volume are made directly.
    32  - Changes to a data volume will not be included when you update an image.
    33  - Data volumes persist even if the container itself is deleted.
    34  
    35  Data volumes are designed to persist data, independent of the container's life 
    36  cycle. Docker therefore *never* automatically delete volumes when you remove 
    37  a container, nor will it "garbage collect" volumes that are no longer 
    38  referenced by a container.
    39  
    40  ### Adding a data volume
    41  
    42  You can add a data volume to a container using the `-v` flag with the
    43  `docker create` and `docker run` command. You can use the `-v` multiple times
    44  to mount multiple data volumes. Let's mount a single volume now in our web
    45  application container.
    46  
    47      $ docker run -d -P --name web -v /webapp training/webapp python app.py
    48  
    49  This will create a new volume inside a container at `/webapp`.
    50  
    51  > **Note:** 
    52  > You can also use the `VOLUME` instruction in a `Dockerfile` to add one or
    53  > more new volumes to any container created from that image.
    54  
    55  ### Locating a volume
    56  
    57  You can locate the volume on the host by utilizing the 'docker inspect' command.
    58  
    59      $ docker inspect web
    60  
    61  The output will provide details on the container configurations including the
    62  volumes. The output should look something similar to the following:
    63  
    64      ...
    65      "Volumes": {
    66          "/webapp": "/var/lib/docker/volumes/fac362...80535"
    67      },
    68      "VolumesRW": {
    69          "/webapp": true
    70      }
    71      ...
    72  
    73  You will notice in the above 'Volumes' is specifying the location on the host and 
    74  'VolumesRW' is specifying that the volume is read/write.
    75  
    76  ### Mount a host directory as a data volume
    77  
    78  In addition to creating a volume using the `-v` flag you can also mount a
    79  directory from your Docker daemon's host into a container.
    80  
    81  > **Note:**
    82  > If you are using Boot2Docker, your Docker daemon only has limited access to
    83  > your OSX/Windows filesystem. Boot2Docker tries to auto-share your `/Users`
    84  > (OSX) or `C:\Users` (Windows) directory - and so you can mount files or directories
    85  > using `docker run -v /Users/<path>:/<container path> ...` (OSX) or
    86  > `docker run -v /c/Users/<path>:/<container path ...` (Windows). All other paths
    87  > come from the Boot2Docker virtual machine's filesystem.
    88  
    89      $ docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
    90  
    91  This will mount the host directory, `/src/webapp`, into the container at
    92  `/opt/webapp`.
    93  
    94  > **Note:**
    95  > If the path `/opt/webapp` already exists inside the container's image, its
    96  > contents will be replaced by the contents of `/src/webapp` on the host to stay
    97  > consistent with the expected behavior of `mount`
    98  
    99  This is very useful for testing, for example we can
   100  mount our source code inside the container and see our application at work as
   101  we change the source code. The directory on the host must be specified as an
   102  absolute path and if the directory doesn't exist Docker will automatically
   103  create it for you.
   104  
   105  > **Note:** 
   106  > This is not available from a `Dockerfile` due to the portability
   107  > and sharing purpose of built images. The host directory is, by its nature,
   108  > host-dependent, so a host directory specified in a `Dockerfile` probably
   109  > wouldn't work on all hosts.
   110  
   111  Docker defaults to a read-write volume but we can also mount a directory
   112  read-only.
   113  
   114      $ docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py
   115  
   116  Here we've mounted the same `/src/webapp` directory but we've added the `ro`
   117  option to specify that the mount should be read-only.
   118  
   119  ### Mount a host file as a data volume
   120  
   121  The `-v` flag can also be used to mount a single file  - instead of *just* 
   122  directories - from the host machine.
   123  
   124      $ docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash
   125  
   126  This will drop you into a bash shell in a new container, you will have your bash 
   127  history from the host and when you exit the container, the host will have the 
   128  history of the commands typed while in the container.
   129  
   130  > **Note:** 
   131  > Many tools used to edit files including `vi` and `sed --in-place` may result 
   132  > in an inode change. Since Docker v1.1.0, this will produce an error such as
   133  > "*sed: cannot rename ./sedKdJ9Dy: Device or resource busy*". In the case where 
   134  > you want to edit the mounted file, it is often easiest to instead mount the 
   135  > parent directory.
   136  
   137  ## Creating and mounting a data volume container
   138  
   139  If you have some persistent data that you want to share between
   140  containers, or want to use from non-persistent containers, it's best to
   141  create a named Data Volume Container, and then to mount the data from
   142  it.
   143  
   144  Let's create a new named container with a volume to share.
   145  While this container doesn't run an application, it reuses the `training/postgres`
   146  image so that all containers are using layers in common, saving disk space.
   147  
   148      $ docker create -v /dbdata --name dbdata training/postgres /bin/true
   149  
   150  You can then use the `--volumes-from` flag to mount the `/dbdata` volume in another container.
   151  
   152      $ docker run -d --volumes-from dbdata --name db1 training/postgres
   153  
   154  And another:
   155  
   156      $ docker run -d --volumes-from dbdata --name db2 training/postgres
   157  
   158  In this case, if the `postgres` image contained a directory called `/dbdata`
   159  then mounting the volumes from the `dbdata` container hides the
   160  `/dbdata` files from the `postgres` image. The result is only the files
   161  from the `dbdata` container are visible.
   162  
   163  You can use multiple `--volumes-from` parameters to bring together multiple data
   164  volumes from multiple containers.
   165  
   166  You can also extend the chain by mounting the volume that came from the
   167  `dbdata` container in yet another container via the `db1` or `db2` containers.
   168  
   169      $ docker run -d --name db3 --volumes-from db1 training/postgres
   170  
   171  If you remove containers that mount volumes, including the initial `dbdata`
   172  container, or the subsequent containers `db1` and `db2`, the volumes will not
   173  be deleted.  To delete the volume from disk, you must explicitly call
   174  `docker rm -v` against the last container with a reference to the volume. This
   175  allows you to upgrade, or effectively migrate data volumes between containers.
   176  
   177  > **Note:** Docker will not warn you when removing a container *without* 
   178  > providing the `-v` option to delete its volumes. If you remove containers
   179  > without using the `-v` option, you may end up with "dangling" volumes; 
   180  > volumes that are no longer referenced by a container.
   181  > Dangling volumes are difficult to get rid of and can take up a large amount
   182  > of disk space. We're working on improving volume management and you can check
   183  > progress on this in [pull request #8484](https://github.com/docker/docker/pull/8484)
   184  
   185  ## Backup, restore, or migrate data volumes
   186  
   187  Another useful function we can perform with volumes is use them for
   188  backups, restores or migrations.  We do this by using the
   189  `--volumes-from` flag to create a new container that mounts that volume,
   190  like so:
   191  
   192      $ docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
   193  
   194  Here we've launched a new container and mounted the volume from the
   195  `dbdata` container. We've then mounted a local host directory as
   196  `/backup`. Finally, we've passed a command that uses `tar` to backup the
   197  contents of the `dbdata` volume to a `backup.tar` file inside our
   198  `/backup` directory. When the command completes and the container stops
   199  we'll be left with a backup of our `dbdata` volume.
   200  
   201  You could then restore it to the same container, or another that you've made
   202  elsewhere. Create a new container.
   203  
   204      $ docker run -v /dbdata --name dbdata2 ubuntu /bin/bash
   205  
   206  Then un-tar the backup file in the new container's data volume.
   207  
   208      $ docker run --volumes-from dbdata2 -v $(pwd):/backup ubuntu cd /dbdata && tar xvf /backup/backup.tar
   209  
   210  You can use the techniques above to automate backup, migration and
   211  restore testing using your preferred tools.
   212  
   213  # Next steps
   214  
   215  Now we've learned a bit more about how to use Docker we're going to see how to
   216  combine Docker with the services available on
   217  [Docker Hub](https://hub.docker.com) including Automated Builds and private
   218  repositories.
   219  
   220  Go to [Working with Docker Hub](/userguide/dockerrepos).
   221