github.com/eikeon/docker@v1.5.0-rc4/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) to provide several useful features for
    25  persistent or shared data:
    26  
    27  - Volumes are initialized when a container is created
    28  - Data volumes can be shared and reused between containers
    29  - Changes to a data volume are made directly
    30  - Changes to a data volume will not be included when you update an image
    31  - Volumes persist until no containers use them
    32  
    33  ### Adding a data volume
    34  
    35  You can add a data volume to a container using the `-v` flag with the
    36  `docker create` and `docker run` command. You can use the `-v` multiple times
    37  to mount multiple data volumes. Let's mount a single volume now in our web
    38  application container.
    39  
    40      $ sudo docker run -d -P --name web -v /webapp training/webapp python app.py
    41  
    42  This will create a new volume inside a container at `/webapp`.
    43  
    44  > **Note:** 
    45  > You can also use the `VOLUME` instruction in a `Dockerfile` to add one or
    46  > more new volumes to any container created from that image.
    47  
    48  ### Mount a Host Directory as a Data Volume
    49  
    50  In addition to creating a volume using the `-v` flag you can also mount a
    51  directory from your Docker daemon's host into a container.
    52  
    53  > **Note:**
    54  > If you are using Boot2Docker, your Docker daemon only has limited access to
    55  > your OSX/Windows filesystem. Boot2Docker tries to auto-share your `/Users`
    56  > (OSX) or `C:\Users` (Windows) directory - and so you can mount files or directories
    57  > using `docker run -v /Users/<path>:/<container path> ...` (OSX) or
    58  > `docker run -v /c/Users/<path>:/<container path ...` (Windows). All other paths
    59  > come from the Boot2Docker virtual machine's filesystem.
    60  
    61      $ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
    62  
    63  This will mount the host directory, `/src/webapp`, into the container at
    64  `/opt/webapp`.
    65  
    66  > **Note:**
    67  > If the path `/opt/webapp` already exists inside the container's image, its
    68  > contents will be replaced by the contents of `/src/webapp` on the host to stay
    69  > consistent with the expected behavior of `mount`
    70  
    71  This is very useful for testing, for example we can
    72  mount our source code inside the container and see our application at work as
    73  we change the source code. The directory on the host must be specified as an
    74  absolute path and if the directory doesn't exist Docker will automatically
    75  create it for you.
    76  
    77  > **Note:** 
    78  > This is not available from a `Dockerfile` due to the portability
    79  > and sharing purpose of built images. The host directory is, by its nature,
    80  > host-dependent, so a host directory specified in a `Dockerfile` probably
    81  > wouldn't work on all hosts.
    82  
    83  Docker defaults to a read-write volume but we can also mount a directory
    84  read-only.
    85  
    86      $ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py
    87  
    88  Here we've mounted the same `/src/webapp` directory but we've added the `ro`
    89  option to specify that the mount should be read-only.
    90  
    91  ### Mount a Host File as a Data Volume
    92  
    93  The `-v` flag can also be used to mount a single file  - instead of *just* 
    94  directories - from the host machine.
    95  
    96      $ sudo docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash
    97  
    98  This will drop you into a bash shell in a new container, you will have your bash 
    99  history from the host and when you exit the container, the host will have the 
   100  history of the commands typed while in the container.
   101  
   102  > **Note:** 
   103  > Many tools used to edit files including `vi` and `sed --in-place` may result 
   104  > in an inode change. Since Docker v1.1.0, this will produce an error such as
   105  > "*sed: cannot rename ./sedKdJ9Dy: Device or resource busy*". In the case where 
   106  > you want to edit the mounted file, it is often easiest to instead mount the 
   107  > parent directory.
   108  
   109  ## Creating and mounting a Data Volume Container
   110  
   111  If you have some persistent data that you want to share between
   112  containers, or want to use from non-persistent containers, it's best to
   113  create a named Data Volume Container, and then to mount the data from
   114  it.
   115  
   116  Let's create a new named container with a volume to share.
   117  While this container doesn't run an application, it reuses the `training/postgres`
   118  image so that all containers are using layers in common, saveing disk space.
   119  
   120      $ sudo docker create -v /dbdata --name dbdata training/postgres
   121  
   122  You can then use the `--volumes-from` flag to mount the `/dbdata` volume in another container.
   123  
   124      $ sudo docker run -d --volumes-from dbdata --name db1 training/postgres
   125  
   126  And another:
   127  
   128      $ sudo docker run -d --volumes-from dbdata --name db2 training/postgres
   129  
   130  You can use multiple `--volumes-from` parameters to bring together multiple data
   131  volumes from multiple containers.
   132  
   133  You can also extend the chain by mounting the volume that came from the
   134  `dbdata` container in yet another container via the `db1` or `db2` containers.
   135  
   136      $ sudo docker run -d --name db3 --volumes-from db1 training/postgres
   137  
   138  If you remove containers that mount volumes, including the initial `dbdata`
   139  container, or the subsequent containers `db1` and `db2`, the volumes will not
   140  be deleted.  To delete the volume from disk, you must explicitly call
   141  `docker rm -v` against the last container with a reference to the volume. This
   142  allows you to upgrade, or effectively migrate data volumes between containers.
   143  
   144  ## Backup, restore, or migrate data volumes
   145  
   146  Another useful function we can perform with volumes is use them for
   147  backups, restores or migrations.  We do this by using the
   148  `--volumes-from` flag to create a new container that mounts that volume,
   149  like so:
   150  
   151      $ sudo docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
   152  
   153  Here we've launched a new container and mounted the volume from the
   154  `dbdata` container. We've then mounted a local host directory as
   155  `/backup`. Finally, we've passed a command that uses `tar` to backup the
   156  contents of the `dbdata` volume to a `backup.tar` file inside our
   157  `/backup` directory. When the command completes and the container stops
   158  we'll be left with a backup of our `dbdata` volume.
   159  
   160  You could then restore it to the same container, or another that you've made
   161  elsewhere. Create a new container.
   162  
   163      $ sudo docker run -v /dbdata --name dbdata2 ubuntu /bin/bash
   164  
   165  Then un-tar the backup file in the new container's data volume.
   166  
   167      $ sudo docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar
   168  
   169  You can use the techniques above to automate backup, migration and
   170  restore testing using your preferred tools.
   171  
   172  # Next steps
   173  
   174  Now we've learned a bit more about how to use Docker we're going to see how to
   175  combine Docker with the services available on
   176  [Docker Hub](https://hub.docker.com) including Automated Builds and private
   177  repositories.
   178  
   179  Go to [Working with Docker Hub](/userguide/dockerrepos).
   180