github.com/a4a881d4/docker@v1.9.0-rc2/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](usingdocker.md), seen how to work with [Docker 16 images](dockerimages.md) as well as learned about [networking 17 and links between containers](dockerlinks.md). 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*](../reference/glossary.md#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 Mounts": [ 78 { 79 "Name": "fac362...80535", 80 "Source": "/var/lib/docker/volumes/fac362...80535/_data", 81 "Destination": "/webapp", 82 "Driver": "local", 83 "Mode": "", 84 "RW": true 85 } 86 ] 87 ... 88 89 You will notice in the above 'Source' is specifying the location on the host and 90 'Destination' is specifying the volume location inside the container. `RW` shows 91 if the volume is read/write. 92 93 ### Mount a host directory as a data volume 94 95 In addition to creating a volume using the `-v` flag you can also mount a 96 directory from your Docker daemon's host into a container. 97 98 ``` 99 $ docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py 100 ``` 101 102 This command mounts the host directory, `/src/webapp`, into the container at 103 `/opt/webapp`. If the path `/opt/webapp` already exists inside the container's 104 image, the `/src/webapp` mount overlays but does not remove the pre-existing 105 content. Once the mount is removed, the content is accessible again. This is 106 consistent with the expected behavior of the `mount` command. 107 108 The `container-dir` must always be an absolute path such as `/src/docs`. 109 The `host-dir` can either be an absolute path or a `name` value. If you 110 supply an absolute path for the `host-dir`, Docker bind-mounts to the path 111 you specify. If you supply a `name`, Docker creates a named volume by that `name`. 112 113 A `name` value must start with start with an alphanumeric character, 114 followed by `a-z0-9`, `_` (underscore), `.` (period) or `-` (hyphen). 115 An absolute path starts with a `/` (forward slash). 116 117 For example, you can specify either `/foo` or `foo` for a `host-dir` value. 118 If you supply the `/foo` value, Docker creates a bind-mount. If you supply 119 the `foo` specification, Docker creates a named volume. 120 121 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 122 to auto-share your `/Users` (OS X) or `C:\Users` (Windows) directory. So, 123 you can mount files or directories on OS X using. 124 125 ``` 126 docker run -v /Users/<path>:/<container path> ... 127 ``` 128 129 On Windows, mount directories using: 130 131 ``` 132 docker run -v /c/Users/<path>:/<container path> ...` 133 ``` 134 135 All other paths come from your virtual machine's filesystem. For example, if 136 you are using VirtualBox some other folder available for sharing, you need to do 137 additional work. In the case of VirtualBox you need to make the host folder 138 available as a shared folder in VirtualBox. Then, you can mount it using the 139 Docker `-v` flag. 140 141 Mounting a host directory can be useful for testing. For example, you can mount 142 source code inside a container. Then, change the source code and see its effect 143 on the application in real time. The directory on the host must be specified as 144 an absolute path and if the directory doesn't exist Docker will automatically 145 create it for you. This auto-creation of the host path has been [*deprecated*](#auto-creating-missing-host-paths-for-bind-mounts). 146 147 Docker volumes default to mount in read-write mode, but you can also set it to 148 be mounted read-only. 149 150 ``` 151 $ docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py 152 ``` 153 154 Here we've mounted the same `/src/webapp` directory but we've added the `ro` 155 option to specify that the mount should be read-only. 156 157 Because of [limitations in the `mount` 158 function](http://lists.linuxfoundation.org/pipermail/containers/2015-April/035788.html), 159 moving subdirectories within the host's source directory can give 160 access from the container to the host's file system. This requires a malicious 161 user with access to host and its mounted directory. 162 163 >**Note**: The host directory is, by its nature, host-dependent. For this 164 >reason, you can't mount a host directory from `Dockerfile` because built images 165 >should be portable. A host directory wouldn't be available on all potential 166 >hosts. 167 168 ### Volume labels 169 170 Labeling systems like SELinux require that proper labels are placed on volume 171 content mounted into a container. Without a label, the security system might 172 prevent the processes running inside the container from using the content. By 173 default, Docker does not change the labels set by the OS. 174 175 To change a label in the container context, you can add either of two suffixes 176 `:z` or `:Z` to the volume mount. These suffixes tell Docker to relabel file 177 objects on the shared volumes. The `z` option tells Docker that two containers 178 share the volume content. As a result, Docker labels the content with a shared 179 content label. Shared volume labels allow all containers to read/write content. 180 The `Z` option tells Docker to label the content with a private unshared label. 181 Only the current container can use a private volume. 182 183 ### Mount a host file as a data volume 184 185 The `-v` flag can also be used to mount a single file - instead of *just* 186 directories - from the host machine. 187 188 $ docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash 189 190 This will drop you into a bash shell in a new container, you will have your bash 191 history from the host and when you exit the container, the host will have the 192 history of the commands typed while in the container. 193 194 > **Note:** 195 > Many tools used to edit files including `vi` and `sed --in-place` may result 196 > in an inode change. Since Docker v1.1.0, this will produce an error such as 197 > "*sed: cannot rename ./sedKdJ9Dy: Device or resource busy*". In the case where 198 > you want to edit the mounted file, it is often easiest to instead mount the 199 > parent directory. 200 201 ## Creating and mounting a data volume container 202 203 If you have some persistent data that you want to share between 204 containers, or want to use from non-persistent containers, it's best to 205 create a named Data Volume Container, and then to mount the data from 206 it. 207 208 Let's create a new named container with a volume to share. 209 While this container doesn't run an application, it reuses the `training/postgres` 210 image so that all containers are using layers in common, saving disk space. 211 212 $ docker create -v /dbdata --name dbdata training/postgres /bin/true 213 214 You can then use the `--volumes-from` flag to mount the `/dbdata` volume in another container. 215 216 $ docker run -d --volumes-from dbdata --name db1 training/postgres 217 218 And another: 219 220 $ docker run -d --volumes-from dbdata --name db2 training/postgres 221 222 In this case, if the `postgres` image contained a directory called `/dbdata` 223 then mounting the volumes from the `dbdata` container hides the 224 `/dbdata` files from the `postgres` image. The result is only the files 225 from the `dbdata` container are visible. 226 227 You can use multiple `--volumes-from` parameters to bring together multiple data 228 volumes from multiple containers. 229 230 You can also extend the chain by mounting the volume that came from the 231 `dbdata` container in yet another container via the `db1` or `db2` containers. 232 233 $ docker run -d --name db3 --volumes-from db1 training/postgres 234 235 If you remove containers that mount volumes, including the initial `dbdata` 236 container, or the subsequent containers `db1` and `db2`, the volumes will not 237 be deleted. To delete the volume from disk, you must explicitly call 238 `docker rm -v` against the last container with a reference to the volume. This 239 allows you to upgrade, or effectively migrate data volumes between containers. 240 241 > **Note:** Docker will not warn you when removing a container *without* 242 > providing the `-v` option to delete its volumes. If you remove containers 243 > without using the `-v` option, you may end up with "dangling" volumes; 244 > volumes that are no longer referenced by a container. 245 > Dangling volumes are difficult to get rid of and can take up a large amount 246 > of disk space. We're working on improving volume management and you can check 247 > progress on this in [pull request #14214](https://github.com/docker/docker/pull/14214) 248 249 ## Backup, restore, or migrate data volumes 250 251 Another useful function we can perform with volumes is use them for 252 backups, restores or migrations. We do this by using the 253 `--volumes-from` flag to create a new container that mounts that volume, 254 like so: 255 256 $ docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata 257 258 Here we've launched a new container and mounted the volume from the 259 `dbdata` container. We've then mounted a local host directory as 260 `/backup`. Finally, we've passed a command that uses `tar` to backup the 261 contents of the `dbdata` volume to a `backup.tar` file inside our 262 `/backup` directory. When the command completes and the container stops 263 we'll be left with a backup of our `dbdata` volume. 264 265 You could then restore it to the same container, or another that you've made 266 elsewhere. Create a new container. 267 268 $ docker run -v /dbdata --name dbdata2 ubuntu /bin/bash 269 270 Then un-tar the backup file in the new container's data volume. 271 272 $ docker run --volumes-from dbdata2 -v $(pwd):/backup ubuntu cd /dbdata && tar xvf /backup/backup.tar 273 274 You can use the techniques above to automate backup, migration and 275 restore testing using your preferred tools. 276 277 # Next steps 278 279 Now we've learned a bit more about how to use Docker we're going to see how to 280 combine Docker with the services available on 281 [Docker Hub](https://hub.docker.com) including Automated Builds and private 282 repositories. 283 284 Go to [Working with Docker Hub](dockerrepos.md).