github.com/vincentwoo/docker@v0.7.3-0.20160116130405-82401a4b13c0/docs/userguide/dockervolumes.md (about) 1 <!--[metadata]> 2 +++ 3 title = "Manage 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 +++ 9 <![end-metadata]--> 10 11 # Manage data in containers 12 13 So far we've been introduced to some [basic Docker concepts](usingdocker.md), 14 seen how to work with [Docker images](dockerimages.md) as well as learned about 15 [networking and links between containers](networking/default_network/dockerlinks.md). In this section we're 16 going to discuss how you can manage data inside and between your Docker 17 containers. 18 19 We're going to look at the two primary ways you can manage data in 20 Docker. 21 22 * Data volumes, and 23 * Data volume containers. 24 25 ## Data volumes 26 27 A *data volume* is a specially-designated directory within one or more 28 containers that bypasses the [*Union File System*](../reference/glossary.md#union-file-system). Data volumes provide several useful features for persistent or shared data: 29 30 - Volumes are initialized when a container is created. If the container's 31 base image contains data at the specified mount point, that existing data is 32 copied into the new volume upon volume initialization. (Note that this does 33 not apply when [mounting a host directory](#mount-a-host-directory-as-a-data-volume).) 34 - Data volumes can be shared and reused among containers. 35 - Changes to a data volume are made directly. 36 - Changes to a data volume will not be included when you update an image. 37 - Data volumes persist even if the container itself is deleted. 38 39 Data volumes are designed to persist data, independent of the container's life 40 cycle. Docker therefore *never* automatically deletes volumes when you remove 41 a container, nor will it "garbage collect" volumes that are no longer 42 referenced by a container. 43 44 ### Adding a data volume 45 46 You can add a data volume to a container using the `-v` flag with the 47 `docker create` and `docker run` command. You can use the `-v` multiple times 48 to mount multiple data volumes. Let's mount a single volume now in our web 49 application container. 50 51 $ docker run -d -P --name web -v /webapp training/webapp python app.py 52 53 This will create a new volume inside a container at `/webapp`. 54 55 > **Note:** 56 > You can also use the `VOLUME` instruction in a `Dockerfile` to add one or 57 > more new volumes to any container created from that image. 58 59 ### Locating a volume 60 61 You can locate the volume on the host by utilizing the `docker inspect` command. 62 63 $ docker inspect web 64 65 The output will provide details on the container configurations including the 66 volumes. The output should look something similar to the following: 67 68 ... 69 Mounts": [ 70 { 71 "Name": "fac362...80535", 72 "Source": "/var/lib/docker/volumes/fac362...80535/_data", 73 "Destination": "/webapp", 74 "Driver": "local", 75 "Mode": "", 76 "RW": true, 77 "Propagation": "" 78 } 79 ] 80 ... 81 82 You will notice in the above `Source` is specifying the location on the host and 83 `Destination` is specifying the volume location inside the container. `RW` shows 84 if the volume is read/write. 85 86 ### Mount a host directory as a data volume 87 88 In addition to creating a volume using the `-v` flag you can also mount a 89 directory from your Docker daemon's host into a container. 90 91 ``` 92 $ docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py 93 ``` 94 95 This command mounts the host directory, `/src/webapp`, into the container at 96 `/opt/webapp`. If the path `/opt/webapp` already exists inside the container's 97 image, the `/src/webapp` mount overlays but does not remove the pre-existing 98 content. Once the mount is removed, the content is accessible again. This is 99 consistent with the expected behavior of the `mount` command. 100 101 The `container-dir` must always be an absolute path such as `/src/docs`. 102 The `host-dir` can either be an absolute path or a `name` value. If you 103 supply an absolute path for the `host-dir`, Docker bind-mounts to the path 104 you specify. If you supply a `name`, Docker creates a named volume by that `name`. 105 106 A `name` value must start with an alphanumeric character, 107 followed by `a-z0-9`, `_` (underscore), `.` (period) or `-` (hyphen). 108 An absolute path starts with a `/` (forward slash). 109 110 For example, you can specify either `/foo` or `foo` for a `host-dir` value. 111 If you supply the `/foo` value, Docker creates a bind-mount. If you supply 112 the `foo` specification, Docker creates a named volume. 113 114 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 115 to auto-share your `/Users` (OS X) or `C:\Users` (Windows) directory. So, 116 you can mount files or directories on OS X using. 117 118 ``` 119 docker run -v /Users/<path>:/<container path> ... 120 ``` 121 122 On Windows, mount directories using: 123 124 ``` 125 docker run -v /c/Users/<path>:/<container path> ...` 126 ``` 127 128 All other paths come from your virtual machine's filesystem. For example, if 129 you are using VirtualBox some other folder available for sharing, you need to do 130 additional work. In the case of VirtualBox you need to make the host folder 131 available as a shared folder in VirtualBox. Then, you can mount it using the 132 Docker `-v` flag. 133 134 Mounting a host directory can be useful for testing. For example, you can mount 135 source code inside a container. Then, change the source code and see its effect 136 on the application in real time. The directory on the host must be specified as 137 an absolute path and if the directory doesn't exist Docker will automatically 138 create it for you. This auto-creation of the host path has been [*deprecated*](#auto-creating-missing-host-paths-for-bind-mounts). 139 140 Docker volumes default to mount in read-write mode, but you can also set it to 141 be mounted read-only. 142 143 ``` 144 $ docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py 145 ``` 146 147 Here we've mounted the same `/src/webapp` directory but we've added the `ro` 148 option to specify that the mount should be read-only. 149 150 Because of [limitations in the `mount` 151 function](http://lists.linuxfoundation.org/pipermail/containers/2015-April/035788.html), 152 moving subdirectories within the host's source directory can give 153 access from the container to the host's file system. This requires a malicious 154 user with access to host and its mounted directory. 155 156 >**Note**: The host directory is, by its nature, host-dependent. For this 157 >reason, you can't mount a host directory from `Dockerfile` because built images 158 >should be portable. A host directory wouldn't be available on all potential 159 >hosts. 160 161 ### Volume labels 162 163 Labeling systems like SELinux require that proper labels are placed on volume 164 content mounted into a container. Without a label, the security system might 165 prevent the processes running inside the container from using the content. By 166 default, Docker does not change the labels set by the OS. 167 168 To change a label in the container context, you can add either of two suffixes 169 `:z` or `:Z` to the volume mount. These suffixes tell Docker to relabel file 170 objects on the shared volumes. The `z` option tells Docker that two containers 171 share the volume content. As a result, Docker labels the content with a shared 172 content label. Shared volume labels allow all containers to read/write content. 173 The `Z` option tells Docker to label the content with a private unshared label. 174 Only the current container can use a private volume. 175 176 ### Mount a host file as a data volume 177 178 The `-v` flag can also be used to mount a single file - instead of *just* 179 directories - from the host machine. 180 181 $ docker run --rm -it -v ~/.bash_history:/root/.bash_history ubuntu /bin/bash 182 183 This will drop you into a bash shell in a new container, you will have your bash 184 history from the host and when you exit the container, the host will have the 185 history of the commands typed while in the container. 186 187 > **Note:** 188 > Many tools used to edit files including `vi` and `sed --in-place` may result 189 > in an inode change. Since Docker v1.1.0, this will produce an error such as 190 > "*sed: cannot rename ./sedKdJ9Dy: Device or resource busy*". In the case where 191 > you want to edit the mounted file, it is often easiest to instead mount the 192 > parent directory. 193 194 ## Creating and mounting a data volume container 195 196 If you have some persistent data that you want to share between 197 containers, or want to use from non-persistent containers, it's best to 198 create a named Data Volume Container, and then to mount the data from 199 it. 200 201 Let's create a new named container with a volume to share. 202 While this container doesn't run an application, it reuses the `training/postgres` 203 image so that all containers are using layers in common, saving disk space. 204 205 $ docker create -v /dbdata --name dbstore training/postgres /bin/true 206 207 You can then use the `--volumes-from` flag to mount the `/dbdata` volume in another container. 208 209 $ docker run -d --volumes-from dbstore --name db1 training/postgres 210 211 And another: 212 213 $ docker run -d --volumes-from dbstore --name db2 training/postgres 214 215 In this case, if the `postgres` image contained a directory called `/dbdata` 216 then mounting the volumes from the `dbstore` container hides the 217 `/dbdata` files from the `postgres` image. The result is only the files 218 from the `dbstore` container are visible. 219 220 You can use multiple `--volumes-from` parameters to combine data volumes from 221 several containers. To find detailed information about `--volumes-from` see the 222 [Mount volumes from container](../reference/commandline/run.md#mount-volumes-from-container-volumes-from) 223 in the `run` command reference. 224 225 You can also extend the chain by mounting the volume that came from the 226 `dbstore` container in yet another container via the `db1` or `db2` containers. 227 228 $ docker run -d --name db3 --volumes-from db1 training/postgres 229 230 If you remove containers that mount volumes, including the initial `dbstore` 231 container, or the subsequent containers `db1` and `db2`, the volumes will not 232 be deleted. To delete the volume from disk, you must explicitly call 233 `docker rm -v` against the last container with a reference to the volume. This 234 allows you to upgrade, or effectively migrate data volumes between containers. 235 236 > **Note:** Docker will not warn you when removing a container *without* 237 > providing the `-v` option to delete its volumes. If you remove containers 238 > without using the `-v` option, you may end up with "dangling" volumes; 239 > volumes that are no longer referenced by a container. 240 > You can use `docker volume ls -f dangling=true` to find dangling volumes, 241 > and use `docker volume rm <volume name>` to remove a volume that's 242 > no longer needed. 243 244 ## Backup, restore, or migrate data volumes 245 246 Another useful function we can perform with volumes is use them for 247 backups, restores or migrations. We do this by using the 248 `--volumes-from` flag to create a new container that mounts that volume, 249 like so: 250 251 $ docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata 252 253 Here we've launched a new container and mounted the volume from the 254 `dbstore` container. We've then mounted a local host directory as 255 `/backup`. Finally, we've passed a command that uses `tar` to backup the 256 contents of the `dbdata` volume to a `backup.tar` file inside our 257 `/backup` directory. When the command completes and the container stops 258 we'll be left with a backup of our `dbdata` volume. 259 260 You could then restore it to the same container, or another that you've made 261 elsewhere. Create a new container. 262 263 $ docker run -v /dbdata --name dbstore2 ubuntu /bin/bash 264 265 Then un-tar the backup file in the new container's data volume. 266 267 $ docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xvf /backup/backup.tar --strip 1" 268 269 You can use the techniques above to automate backup, migration and 270 restore testing using your preferred tools. 271 272 ## Important tips on using shared volumes 273 274 Multiple containers can also share one or more data volumes. However, multiple containers writing to a single shared volume can cause data corruption. Make sure your applications are designed to write to shared data stores. 275 276 Data volumes are directly accessible from the Docker host. This means you can read and write to them with normal Linux tools. In most cases you should not do this as it can cause data corruption if your containers and applications are unaware of your direct access. 277 278 # Next steps 279 280 Now we've learned a bit more about how to use Docker we're going to see how to 281 combine Docker with the services available on 282 [Docker Hub](https://hub.docker.com) including Automated Builds and private 283 repositories. 284 285 Go to [Working with Docker Hub](dockerrepos.md).