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