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