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