github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/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*](/reference/glossary#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. 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/ 159 035788.html), 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 169 ### Mount a host file as a data volume 170 171 The `-v` flag can also be used to mount a single file - instead of *just* 172 directories - from the host machine. 173 174 $ docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash 175 176 This will drop you into a bash shell in a new container, you will have your bash 177 history from the host and when you exit the container, the host will have the 178 history of the commands typed while in the container. 179 180 > **Note:** 181 > Many tools used to edit files including `vi` and `sed --in-place` may result 182 > in an inode change. Since Docker v1.1.0, this will produce an error such as 183 > "*sed: cannot rename ./sedKdJ9Dy: Device or resource busy*". In the case where 184 > you want to edit the mounted file, it is often easiest to instead mount the 185 > parent directory. 186 187 ## Creating and mounting a data volume container 188 189 If you have some persistent data that you want to share between 190 containers, or want to use from non-persistent containers, it's best to 191 create a named Data Volume Container, and then to mount the data from 192 it. 193 194 Let's create a new named container with a volume to share. 195 While this container doesn't run an application, it reuses the `training/postgres` 196 image so that all containers are using layers in common, saving disk space. 197 198 $ docker create -v /dbdata --name dbdata training/postgres /bin/true 199 200 You can then use the `--volumes-from` flag to mount the `/dbdata` volume in another container. 201 202 $ docker run -d --volumes-from dbdata --name db1 training/postgres 203 204 And another: 205 206 $ docker run -d --volumes-from dbdata --name db2 training/postgres 207 208 In this case, if the `postgres` image contained a directory called `/dbdata` 209 then mounting the volumes from the `dbdata` container hides the 210 `/dbdata` files from the `postgres` image. The result is only the files 211 from the `dbdata` container are visible. 212 213 You can use multiple `--volumes-from` parameters to bring together multiple data 214 volumes from multiple containers. 215 216 You can also extend the chain by mounting the volume that came from the 217 `dbdata` container in yet another container via the `db1` or `db2` containers. 218 219 $ docker run -d --name db3 --volumes-from db1 training/postgres 220 221 If you remove containers that mount volumes, including the initial `dbdata` 222 container, or the subsequent containers `db1` and `db2`, the volumes will not 223 be deleted. To delete the volume from disk, you must explicitly call 224 `docker rm -v` against the last container with a reference to the volume. This 225 allows you to upgrade, or effectively migrate data volumes between containers. 226 227 > **Note:** Docker will not warn you when removing a container *without* 228 > providing the `-v` option to delete its volumes. If you remove containers 229 > without using the `-v` option, you may end up with "dangling" volumes; 230 > volumes that are no longer referenced by a container. 231 > Dangling volumes are difficult to get rid of and can take up a large amount 232 > of disk space. We're working on improving volume management and you can check 233 > progress on this in [pull request #14214](https://github.com/docker/docker/pull/14214) 234 235 ## Backup, restore, or migrate data volumes 236 237 Another useful function we can perform with volumes is use them for 238 backups, restores or migrations. We do this by using the 239 `--volumes-from` flag to create a new container that mounts that volume, 240 like so: 241 242 $ docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata 243 244 Here we've launched a new container and mounted the volume from the 245 `dbdata` container. We've then mounted a local host directory as 246 `/backup`. Finally, we've passed a command that uses `tar` to backup the 247 contents of the `dbdata` volume to a `backup.tar` file inside our 248 `/backup` directory. When the command completes and the container stops 249 we'll be left with a backup of our `dbdata` volume. 250 251 You could then restore it to the same container, or another that you've made 252 elsewhere. Create a new container. 253 254 $ docker run -v /dbdata --name dbdata2 ubuntu /bin/bash 255 256 Then un-tar the backup file in the new container's data volume. 257 258 $ docker run --volumes-from dbdata2 -v $(pwd):/backup ubuntu cd /dbdata && tar xvf /backup/backup.tar 259 260 You can use the techniques above to automate backup, migration and 261 restore testing using your preferred tools. 262 263 # Next steps 264 265 Now we've learned a bit more about how to use Docker we're going to see how to 266 combine Docker with the services available on 267 [Docker Hub](https://hub.docker.com) including Automated Builds and private 268 repositories. 269 270 Go to [Working with Docker Hub](/userguide/dockerrepos).