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