github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/docs/userguide/containers/dockervolumes.md (about)

     1  <!--[metadata]>
     2  +++
     3  aliases = ["/engine/userguide/dockervolumes/"]
     4  title = "Manage data in containers"
     5  description = "How to manage data inside your Docker containers."
     6  keywords = ["Examples, Usage, volume, docker, documentation, user guide, data,  volumes"]
     7  [menu.main]
     8  parent = "engine_learn"
     9  +++
    10  <![end-metadata]-->
    11  
    12  # Manage data in containers
    13  
    14  So far we've been introduced to some [basic Docker concepts](../containers/usingdocker.md),
    15  seen how to work with [Docker images](../containers/dockerimages.md) as well as learned about
    16  [networking and links between containers](../networking/default_network/dockerlinks.md). In this section we're
    17  going to discuss how you can manage data inside and between your Docker
    18  containers.
    19  
    20  We're going to look at the two primary ways you can manage data in
    21  Docker.
    22  
    23  * Data volumes, and
    24  * Data volume containers.
    25  
    26  ## Data volumes
    27  
    28  A *data volume* is a specially-designated directory within one or more
    29  containers that bypasses the [*Union File System*](../../reference/glossary.md#union-file-system). Data volumes provide several useful features for persistent or shared data:
    30  
    31  - Volumes are initialized when a container is created. If the container's
    32    base image contains data at the specified mount point, that existing data is
    33    copied into the new volume upon volume initialization. (Note that this does
    34    not apply when [mounting a host directory](#mount-a-host-directory-as-a-data-volume).)
    35  - Data volumes can be shared and reused among containers.
    36  - Changes to a data volume are made directly.
    37  - Changes to a data volume will not be included when you update an image.
    38  - Data volumes persist even if the container itself is deleted.
    39  
    40  Data volumes are designed to persist data, independent of the container's life
    41  cycle. Docker therefore *never* automatically deletes volumes when you remove
    42  a container, nor will it "garbage collect" volumes that are no longer
    43  referenced by a container.
    44  
    45  ### Adding a data volume
    46  
    47  You can add a data volume to a container using the `-v` flag with the
    48  `docker create` and `docker run` command. You can use the `-v` multiple times
    49  to mount multiple data volumes. Let's mount a single volume now in our web
    50  application container.
    51  
    52      $ docker run -d -P --name web -v /webapp training/webapp python app.py
    53  
    54  This will create a new volume inside a container at `/webapp`.
    55  
    56  > **Note:**
    57  > You can also use the `VOLUME` instruction in a `Dockerfile` to add one or
    58  > more new volumes to any container created from that image.
    59  
    60  ### Locating a volume
    61  
    62  You can locate the volume on the host by utilizing the `docker inspect` command.
    63  
    64      $ docker inspect web
    65  
    66  The output will provide details on the container configurations including the
    67  volumes. The output should look something similar to the following:
    68  
    69      ...
    70      Mounts": [
    71          {
    72              "Name": "fac362...80535",
    73              "Source": "/var/lib/docker/volumes/fac362...80535/_data",
    74              "Destination": "/webapp",
    75              "Driver": "local",
    76              "Mode": "",
    77              "RW": true,
    78              "Propagation": ""
    79          }
    80      ]
    81      ...
    82  
    83  You will notice in the above `Source` is specifying the location on the host and
    84  `Destination` is specifying the volume location inside the container. `RW` shows
    85  if the volume is read/write.
    86  
    87  ### Mount a host directory as a data volume
    88  
    89  In addition to creating a volume using the `-v` flag you can also mount a
    90  directory from your Docker daemon's host into a container.
    91  
    92  ```
    93  $ docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
    94  ```
    95  
    96  This command mounts the host directory, `/src/webapp`, into the container at
    97  `/opt/webapp`.  If the path `/opt/webapp` already exists inside the container's
    98  image, the `/src/webapp` mount overlays but does not remove the pre-existing
    99  content. Once the mount is removed, the content is accessible again. This is
   100  consistent with the expected behavior of the `mount` command.
   101  
   102  The `container-dir` must always be an absolute path such as `/src/docs`.
   103  The `host-dir` can either be an absolute path or a `name` value. If you
   104  supply an absolute path for the `host-dir`, Docker bind-mounts to the path
   105  you specify. If you supply a `name`, Docker creates a named volume by that `name`.
   106  
   107  A `name` value must start with an alphanumeric character,
   108  followed by `a-z0-9`, `_` (underscore), `.` (period) or `-` (hyphen).
   109  An absolute path starts with a `/` (forward slash).
   110  
   111  For example, you can specify either `/foo` or `foo` for a `host-dir` value.
   112  If you supply the `/foo` value, Docker creates a bind-mount. If you supply
   113  the `foo` specification, Docker creates a named volume.
   114  
   115  If you are using Docker Machine on Mac or Windows, your Docker daemon has only limited access to your OS X or Windows filesystem. Docker Machine tries
   116  to auto-share your `/Users` (OS X) or `C:\Users` (Windows) directory.  So,
   117  you can mount files or directories on OS X using.
   118  
   119  ```
   120  docker run -v /Users/<path>:/<container path> ...
   121  ```
   122  
   123  On Windows, mount directories using:
   124  
   125  ```
   126  docker run -v /c/Users/<path>:/<container path> ...`
   127  ```
   128  
   129  All other paths come from your virtual machine's filesystem.  For example, if
   130  you are using VirtualBox some other folder available for sharing, you need to do
   131  additional work. In the case of VirtualBox you need to make the host folder
   132  available as a shared folder in VirtualBox. Then, you can mount it using the
   133  Docker `-v` flag.
   134  
   135  Mounting a host directory can be useful for testing. For example, you can mount
   136  source code inside a container. Then, change the source code and see its effect
   137  on the application in real time. The directory on the host must be specified as
   138  an absolute path and if the directory doesn't exist Docker will automatically
   139  create it for you.  This auto-creation of the host path has been [*deprecated*](#auto-creating-missing-host-paths-for-bind-mounts).
   140  
   141  Docker volumes default to mount in read-write mode, but you can also set it to
   142  be mounted read-only.
   143  
   144  ```
   145  $ docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py
   146  ```
   147  
   148  Here we've mounted the same `/src/webapp` directory but we've added the `ro`
   149  option to specify that the mount should be read-only.
   150  
   151  Because of [limitations in the `mount`
   152  function](http://lists.linuxfoundation.org/pipermail/containers/2015-April/035788.html),
   153  moving subdirectories within the host's source directory can give
   154  access from the container to the host's file system. This requires a malicious
   155  user with access to host and its mounted directory.
   156  
   157  >**Note**: The host directory is, by its nature, host-dependent. For this
   158  >reason, you can't mount a host directory from `Dockerfile` because built images
   159  >should be portable. A host directory wouldn't be available on all potential
   160  >hosts.
   161  
   162  ### Volume labels
   163  
   164  Labeling systems like SELinux require that proper labels are placed on volume
   165  content mounted into a container. Without a label, the security system might
   166  prevent the processes running inside the container from using the content. By
   167  default, Docker does not change the labels set by the OS.
   168  
   169  To change a label in the container context, you can add either of two suffixes
   170  `:z` or `:Z` to the volume mount. These suffixes tell Docker to relabel file
   171  objects on the shared volumes. The `z` option tells Docker that two containers
   172  share the volume content. As a result, Docker labels the content with a shared
   173  content label. Shared volume labels allow all containers to read/write content.
   174  The `Z` option tells Docker to label the content with a private unshared label.
   175  Only the current container can use a private volume.
   176  
   177  ### Mount a host file as a data volume
   178  
   179  The `-v` flag can also be used to mount a single file  - instead of *just*
   180  directories - from the host machine.
   181  
   182      $ docker run --rm -it -v ~/.bash_history:/root/.bash_history ubuntu /bin/bash
   183  
   184  This will drop you into a bash shell in a new container, you will have your bash
   185  history from the host and when you exit the container, the host will have the
   186  history of the commands typed while in the container.
   187  
   188  > **Note:**
   189  > Many tools used to edit files including `vi` and `sed --in-place` may result
   190  > in an inode change. Since Docker v1.1.0, this will produce an error such as
   191  > "*sed: cannot rename ./sedKdJ9Dy: Device or resource busy*". In the case where
   192  > you want to edit the mounted file, it is often easiest to instead mount the
   193  > parent directory.
   194  
   195  ## Creating and mounting a data volume container
   196  
   197  If you have some persistent data that you want to share between
   198  containers, or want to use from non-persistent containers, it's best to
   199  create a named Data Volume Container, and then to mount the data from
   200  it.
   201  
   202  Let's create a new named container with a volume to share.
   203  While this container doesn't run an application, it reuses the `training/postgres`
   204  image so that all containers are using layers in common, saving disk space.
   205  
   206      $ docker create -v /dbdata --name dbstore training/postgres /bin/true
   207  
   208  You can then use the `--volumes-from` flag to mount the `/dbdata` volume in another container.
   209  
   210      $ docker run -d --volumes-from dbstore --name db1 training/postgres
   211  
   212  And another:
   213  
   214      $ docker run -d --volumes-from dbstore --name db2 training/postgres
   215  
   216  In this case, if the `postgres` image contained a directory called `/dbdata`
   217  then mounting the volumes from the `dbstore` container hides the
   218  `/dbdata` files from the `postgres` image. The result is only the files
   219  from the `dbstore` container are visible.
   220  
   221  You can use multiple `--volumes-from` parameters to combine data volumes from
   222  several containers. To find detailed information about `--volumes-from` see the
   223  [Mount volumes from container](../../reference/commandline/run.md#mount-volumes-from-container-volumes-from)
   224  in the `run` command reference.
   225  
   226  You can also extend the chain by mounting the volume that came from the
   227  `dbstore` container in yet another container via the `db1` or `db2` containers.
   228  
   229      $ docker run -d --name db3 --volumes-from db1 training/postgres
   230  
   231  If you remove containers that mount volumes, including the initial `dbstore`
   232  container, or the subsequent containers `db1` and `db2`, the volumes will not
   233  be deleted.  To delete the volume from disk, you must explicitly call
   234  `docker rm -v` against the last container with a reference to the volume. This
   235  allows you to upgrade, or effectively migrate data volumes between containers.
   236  
   237  > **Note:** Docker will not warn you when removing a container *without*
   238  > providing the `-v` option to delete its volumes. If you remove containers
   239  > without using the `-v` option, you may end up with "dangling" volumes;
   240  > volumes that are no longer referenced by a container.
   241  > You can use `docker volume ls -f dangling=true` to find dangling volumes,
   242  > and use `docker volume rm <volume name>` to remove a volume that's
   243  > no longer needed.
   244  
   245  ## Backup, restore, or migrate data volumes
   246  
   247  Another useful function we can perform with volumes is use them for
   248  backups, restores or migrations.  We do this by using the
   249  `--volumes-from` flag to create a new container that mounts that volume,
   250  like so:
   251  
   252      $ docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
   253  
   254  Here we've launched a new container and mounted the volume from the
   255  `dbstore` container. We've then mounted a local host directory as
   256  `/backup`. Finally, we've passed a command that uses `tar` to backup the
   257  contents of the `dbdata` volume to a `backup.tar` file inside our
   258  `/backup` directory. When the command completes and the container stops
   259  we'll be left with a backup of our `dbdata` volume.
   260  
   261  You could then restore it to the same container, or another that you've made
   262  elsewhere. Create a new container.
   263  
   264      $ docker run -v /dbdata --name dbstore2 ubuntu /bin/bash
   265  
   266  Then un-tar the backup file in the new container's data volume.
   267  
   268      $ docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xvf /backup/backup.tar --strip 1"
   269  
   270  You can use the techniques above to automate backup, migration and
   271  restore testing using your preferred tools.
   272  
   273  ## Important tips on using shared volumes
   274  
   275  Multiple containers can also share one or more data volumes. However, multiple containers writing to a single shared volume can cause data corruption. Make sure your applications are designed to write to shared data stores.
   276  
   277  Data volumes are directly accessible from the Docker host. This means you can read and write to them with normal Linux tools. In most cases you should not do this as it can cause data corruption if your containers and applications are unaware of your direct access.
   278  
   279  # Next steps
   280  
   281  Now we've learned a bit more about how to use Docker we're going to see how to
   282  combine Docker with the services available on
   283  [Docker Hub](https://hub.docker.com) including Automated Builds and private
   284  repositories.
   285  
   286  Go to [Working with Docker Hub](../containers/dockerrepos.md).