github.com/slava-ustovytski/docker@v1.8.2-rc1/docs/userguide/dockervolumes.md (about)

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