2015-06-07 23:07:20 -04:00
<!-- [metadata]>
+++
2016-06-13 14:08:11 -04:00
aliases = [
"/engine/userguide/containers/dockervolumes/",
"/engine/userguide/dockervolumes/"
]
2015-09-30 16:11:36 -04:00
title = "Manage data in containers"
2015-06-07 23:07:20 -04:00
description = "How to manage data inside your Docker containers."
keywords = ["Examples, Usage, volume, docker, documentation, user guide, data, volumes"]
[menu.main]
2016-06-17 01:29:56 -04:00
parent = "engine_learn_menu"
2015-06-07 23:07:20 -04:00
+++
< ![end-metadata]-->
2014-05-21 17:05:19 -04:00
2015-09-30 16:11:36 -04:00
# Manage data in containers
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
So far you've been introduced to some [basic Docker
2016-06-13 14:08:11 -04:00
concepts](usingdocker.md), seen how to work with [Docker
images](dockerimages.md) as well as learned about [networking and
links between containers](../userguide/networking/default_network/dockerlinks.md). In this
2016-02-08 16:37:21 -05:00
section you're going to learn how you can manage data inside and between your
Docker containers.
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
You're going to look at the two primary ways you can manage data with
Docker Engine.
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
* Data volumes
* Data volume containers
2014-05-21 17:05:19 -04:00
## Data volumes
A *data volume* is a specially-designated directory within one or more
2016-06-13 14:08:11 -04:00
containers that bypasses the [*Union File System* ](../reference/glossary.md#union-file-system ). Data volumes provide several useful features for persistent or shared data:
2014-05-21 17:05:19 -04:00
2015-03-06 09:36:23 -05:00
- Volumes are initialized when a container is created. If the container's
2015-09-30 16:11:36 -04:00
base image contains data at the specified mount point, that existing data is
2015-12-15 14:23:56 -05:00
copied into the new volume upon volume initialization. (Note that this does
not apply when [mounting a host directory ](#mount-a-host-directory-as-a-data-volume ).)
2015-03-06 09:36:23 -05:00
- Data volumes can be shared and reused among containers.
- Changes to a data volume are made directly.
- Changes to a data volume will not be included when you update an image.
- Data volumes persist even if the container itself is deleted.
2015-02-12 19:17:33 -05:00
2015-09-30 16:11:36 -04:00
Data volumes are designed to persist data, independent of the container's life
2015-11-05 10:03:35 -05:00
cycle. Docker therefore *never* automatically deletes volumes when you remove
2015-09-30 16:11:36 -04:00
a container, nor will it "garbage collect" volumes that are no longer
2015-03-06 09:36:23 -05:00
referenced by a container.
2014-05-21 17:05:19 -04:00
### Adding a data volume
You can add a data volume to a container using the `-v` flag with the
2014-12-04 23:31:54 -05:00
`docker create` and `docker run` command. You can use the `-v` multiple times
2016-02-08 16:37:21 -05:00
to mount multiple data volumes. Now, mount a single volume in your web
2014-12-04 23:31:54 -05:00
application container.
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
```bash
$ docker run -d -P --name web -v /webapp training/webapp python app.py
```
2014-05-21 17:05:19 -04:00
This will create a new volume inside a container at `/webapp` .
2015-09-30 16:11:36 -04:00
> **Note:**
2014-05-21 17:05:19 -04:00
> You can also use the `VOLUME` instruction in a `Dockerfile` to add one or
> more new volumes to any container created from that image.
2015-03-23 23:16:40 -04:00
### Locating a volume
2015-12-20 00:29:29 -05:00
You can locate the volume on the host by utilizing the `docker inspect` command.
2015-03-23 23:16:40 -04:00
2016-02-08 16:37:21 -05:00
```bash
$ docker inspect web
```
2015-03-23 23:16:40 -04:00
The output will provide details on the container configurations including the
volumes. The output should look something similar to the following:
2016-02-08 16:37:21 -05:00
```json
...
"Mounts": [
{
"Name": "fac362...80535",
"Source": "/var/lib/docker/volumes/fac362...80535/_data",
"Destination": "/webapp",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
...
```
2015-03-23 23:16:40 -04:00
2015-12-20 00:29:29 -05:00
You will notice in the above `Source` is specifying the location on the host and
`Destination` is specifying the volume location inside the container. `RW` shows
2015-08-18 15:16:26 -04:00
if the volume is read/write.
2015-03-23 23:16:40 -04:00
2015-04-21 11:50:09 -04:00
### Mount a host directory as a data volume
2014-05-21 17:05:19 -04:00
In addition to creating a volume using the `-v` flag you can also mount a
2016-07-10 00:26:58 -04:00
directory from your Docker engine's host into a container.
2014-12-03 20:12:16 -05:00
2016-02-08 16:37:21 -05:00
```bash
2015-08-28 20:37:03 -04:00
$ docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
```
This command mounts the host directory, `/src/webapp` , into the container at
`/opt/webapp` . If the path `/opt/webapp` already exists inside the container's
image, the `/src/webapp` mount overlays but does not remove the pre-existing
content. Once the mount is removed, the content is accessible again. This is
consistent with the expected behavior of the `mount` command.
2015-09-30 16:11:36 -04:00
The `container-dir` must always be an absolute path such as `/src/docs` .
The `host-dir` can either be an absolute path or a `name` value. If you
supply an absolute path for the `host-dir` , Docker bind-mounts to the path
2015-09-05 23:09:29 -04:00
you specify. If you supply a `name` , Docker creates a named volume by that `name` .
2015-12-20 00:29:29 -05:00
A `name` value must start with an alphanumeric character,
2015-09-30 16:11:36 -04:00
followed by `a-z0-9` , `_` (underscore), `.` (period) or `-` (hyphen).
2015-09-05 23:09:29 -04:00
An absolute path starts with a `/` (forward slash).
2015-09-30 16:11:36 -04:00
For example, you can specify either `/foo` or `foo` for a `host-dir` value.
2016-07-10 00:26:58 -04:00
If you supply the `/foo` value, the Docker Engine creates a bind-mount. If you supply
the `foo` specification, the Docker Engine creates a named volume.
2015-09-05 23:09:29 -04:00
2016-07-10 00:26:58 -04:00
If you are using Docker Machine on Mac or Windows, your Docker Engine daemon has only
2016-02-08 16:37:21 -05:00
limited access to your OS X or Windows filesystem. Docker Machine tries to
auto-share your `/Users` (OS X) or `C:\Users` (Windows) directory. So, you can
mount files or directories on OS X using.
2015-08-28 20:37:03 -04:00
2016-02-08 16:37:21 -05:00
```bash
2015-08-28 20:37:03 -04:00
docker run -v /Users/< path > :/< container path > ...
```
On Windows, mount directories using:
2016-02-08 16:37:21 -05:00
```bash
2015-09-30 16:11:36 -04:00
docker run -v /c/Users/< path > :/< container path > ...`
2015-08-28 20:37:03 -04:00
```
2016-02-06 06:27:20 -05:00
All other paths come from your virtual machine's filesystem, so if you want
to make some other host folder available for sharing, you need to do
2015-08-28 20:37:03 -04:00
additional work. In the case of VirtualBox you need to make the host folder
available as a shared folder in VirtualBox. Then, you can mount it using the
Docker `-v` flag.
Mounting a host directory can be useful for testing. For example, you can mount
source code inside a container. Then, change the source code and see its effect
on the application in real time. The directory on the host must be specified as
2016-07-10 00:26:58 -04:00
an absolute path and if the directory doesn't exist the Docker Engine daemon
automatically creates it for you.
2014-05-21 17:05:19 -04:00
2015-08-28 20:37:03 -04:00
Docker volumes default to mount in read-write mode, but you can also set it to
be mounted read-only.
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
```bash
2015-08-28 20:37:03 -04:00
$ docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py
```
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
Here you've mounted the same `/src/webapp` directory but you've added the `ro`
2014-05-21 17:05:19 -04:00
option to specify that the mount should be read-only.
2015-07-31 17:26:40 -04:00
Because of [limitations in the `mount`
2015-10-22 02:59:16 -04:00
function](http://lists.linuxfoundation.org/pipermail/containers/2015-April/035788.html),
moving subdirectories within the host's source directory can give
2015-07-31 17:26:40 -04:00
access from the container to the host's file system. This requires a malicious
2015-09-30 16:11:36 -04:00
user with access to host and its mounted directory.
2015-07-31 17:26:40 -04:00
2015-08-28 20:37:03 -04:00
>**Note**: The host directory is, by its nature, host-dependent. For this
>reason, you can't mount a host directory from `Dockerfile` because built images
>should be portable. A host directory wouldn't be available on all potential
>hosts.
2016-02-23 14:43:37 -05:00
### Mount a shared-storage volume as a data volume
In addition to mounting a host directory in your container, some Docker
2016-06-13 14:08:11 -04:00
[volume plugins ](../extend/plugins_volume.md ) allow you to
2016-02-23 14:43:37 -05:00
provision and mount shared storage, such as iSCSI, NFS, or FC.
A benefit of using shared volumes is that they are host-independent. This
means that a volume can be made available on any host that a container is
started on as long as it has access to the shared storage backend, and has
the plugin installed.
2016-02-08 16:37:21 -05:00
One way to use volume drivers is through the `docker run` command.
2016-02-23 14:43:37 -05:00
Volume drivers create volumes by name, instead of by path like in
the other examples.
The following command creates a named volume, called `my-named-volume` ,
using the `flocker` volume driver, and makes it available within the container
at `/opt/webapp` :
```bash
$ docker run -d -P \
--volume-driver=flocker \
-v my-named-volume:/opt/webapp \
--name web training/webapp python app.py
```
You may also use the `docker volume create` command, to create a volume before
using it in a container.
The following example also creates the `my-named-volume` volume, this time
using the `docker volume create` command.
```bash
$ docker volume create -d flocker --name my-named-volume -o size=20GB
2016-06-30 19:46:57 -04:00
2016-02-23 14:43:37 -05:00
$ docker run -d -P \
-v my-named-volume:/opt/webapp \
--name web training/webapp python app.py
```
A list of available plugins, including volume plugins, is available
2016-06-13 14:08:11 -04:00
[here ](../extend/plugins.md ).
2016-02-23 14:43:37 -05:00
2015-09-14 15:42:31 -04:00
### Volume labels
Labeling systems like SELinux require that proper labels are placed on volume
content mounted into a container. Without a label, the security system might
prevent the processes running inside the container from using the content. By
default, Docker does not change the labels set by the OS.
To change a label in the container context, you can add either of two suffixes
`:z` or `:Z` to the volume mount. These suffixes tell Docker to relabel file
objects on the shared volumes. The `z` option tells Docker that two containers
share the volume content. As a result, Docker labels the content with a shared
content label. Shared volume labels allow all containers to read/write content.
The `Z` option tells Docker to label the content with a private unshared label.
Only the current container can use a private volume.
2015-07-31 17:26:40 -04:00
2015-04-21 11:50:09 -04:00
### Mount a host file as a data volume
2014-07-04 13:52:10 -04:00
2015-09-30 16:11:36 -04:00
The `-v` flag can also be used to mount a single file - instead of *just*
2014-07-09 03:21:31 -04:00
directories - from the host machine.
2014-07-04 13:52:10 -04:00
2016-02-08 16:37:21 -05:00
```bash
$ docker run --rm -it -v ~/.bash_history:/root/.bash_history ubuntu /bin/bash
```
2014-07-04 13:52:10 -04:00
2015-09-30 16:11:36 -04:00
This will drop you into a bash shell in a new container, you will have your bash
history from the host and when you exit the container, the host will have the
2014-07-07 07:01:04 -04:00
history of the commands typed while in the container.
2014-07-04 13:52:10 -04:00
2015-09-30 16:11:36 -04:00
> **Note:**
> Many tools used to edit files including `vi` and `sed --in-place` may result
2014-07-09 03:21:31 -04:00
> in an inode change. Since Docker v1.1.0, this will produce an error such as
2015-09-30 16:11:36 -04:00
> "*sed: cannot rename ./sedKdJ9Dy: Device or resource busy*". In the case where
> you want to edit the mounted file, it is often easiest to instead mount the
2014-07-07 07:01:04 -04:00
> parent directory.
2014-07-04 13:52:10 -04:00
2015-04-21 11:50:09 -04:00
## Creating and mounting a data volume container
2014-05-21 17:05:19 -04:00
If you have some persistent data that you want to share between
containers, or want to use from non-persistent containers, it's best to
create a named Data Volume Container, and then to mount the data from
it.
Let's create a new named container with a volume to share.
2014-12-04 23:31:54 -05:00
While this container doesn't run an application, it reuses the `training/postgres`
2015-01-31 11:02:11 -05:00
image so that all containers are using layers in common, saving disk space.
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
```bash
$ docker create -v /dbdata --name dbstore training/postgres /bin/true
```
2014-05-21 17:05:19 -04:00
You can then use the `--volumes-from` flag to mount the `/dbdata` volume in another container.
2016-02-08 16:37:21 -05:00
```bash
$ docker run -d --volumes-from dbstore --name db1 training/postgres
```
2014-05-21 17:05:19 -04:00
And another:
2016-02-08 16:37:21 -05:00
```bash
$ docker run -d --volumes-from dbstore --name db2 training/postgres
```
2014-05-21 17:05:19 -04:00
2015-03-06 09:36:23 -05:00
In this case, if the `postgres` image contained a directory called `/dbdata`
2015-10-28 17:14:49 -04:00
then mounting the volumes from the `dbstore` container hides the
2015-03-06 09:36:23 -05:00
`/dbdata` files from the `postgres` image. The result is only the files
2015-10-28 17:14:49 -04:00
from the `dbstore` container are visible.
2015-03-06 09:36:23 -05:00
2015-11-11 21:23:49 -05:00
You can use multiple `--volumes-from` parameters to combine data volumes from
several containers. To find detailed information about `--volumes-from` see the
2016-06-13 14:08:11 -04:00
[Mount volumes from container ](../reference/commandline/run.md#mount-volumes-from-container-volumes-from )
2015-11-11 21:23:49 -05:00
in the `run` command reference.
2014-05-21 17:05:19 -04:00
You can also extend the chain by mounting the volume that came from the
2015-10-28 17:14:49 -04:00
`dbstore` container in yet another container via the `db1` or `db2` containers.
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
```bash
$ docker run -d --name db3 --volumes-from db1 training/postgres
```
2014-05-21 17:05:19 -04:00
2015-10-28 17:14:49 -04:00
If you remove containers that mount volumes, including the initial `dbstore`
2014-05-21 17:05:19 -04:00
container, or the subsequent containers `db1` and `db2` , the volumes will not
2014-08-28 16:41:17 -04:00
be deleted. To delete the volume from disk, you must explicitly call
`docker rm -v` against the last container with a reference to the volume. This
2014-05-21 17:05:19 -04:00
allows you to upgrade, or effectively migrate data volumes between containers.
2015-09-30 16:11:36 -04:00
> **Note:** Docker will not warn you when removing a container *without*
2015-02-12 19:17:33 -05:00
> providing the `-v` option to delete its volumes. If you remove containers
2015-09-30 16:11:36 -04:00
> without using the `-v` option, you may end up with "dangling" volumes;
2015-02-12 19:17:33 -05:00
> volumes that are no longer referenced by a container.
2016-01-07 04:28:30 -05:00
> You can use `docker volume ls -f dangling=true` to find dangling volumes,
> and use `docker volume rm <volume name>` to remove a volume that's
> no longer needed.
2015-02-12 19:17:33 -05:00
2014-05-21 17:05:19 -04:00
## Backup, restore, or migrate data volumes
Another useful function we can perform with volumes is use them for
2016-02-08 16:37:21 -05:00
backups, restores or migrations. You do this by using the
2014-05-21 17:05:19 -04:00
`--volumes-from` flag to create a new container that mounts that volume,
like so:
2016-02-08 16:37:21 -05:00
```bash
$ docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
```
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
Here you've launched a new container and mounted the volume from the
`dbstore` container. You've then mounted a local host directory as
`/backup` . Finally, you've passed a command that uses `tar` to backup the
2014-05-21 17:05:19 -04:00
contents of the `dbdata` volume to a `backup.tar` file inside our
`/backup` directory. When the command completes and the container stops
we'll be left with a backup of our `dbdata` volume.
2014-07-25 03:06:38 -04:00
You could then restore it to the same container, or another that you've made
2014-05-21 17:05:19 -04:00
elsewhere. Create a new container.
2016-02-08 16:37:21 -05:00
```bash
$ docker run -v /dbdata --name dbstore2 ubuntu /bin/bash
```
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
Then un-tar the backup file in the new container`s data volume.
2014-05-21 17:05:19 -04:00
2016-02-08 16:37:21 -05:00
```bash
$ docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata & & tar xvf /backup/backup.tar --strip 1"
```
2014-05-21 17:05:19 -04:00
2014-09-11 02:55:45 -04:00
You can use the techniques above to automate backup, migration and
2014-05-21 17:05:19 -04:00
restore testing using your preferred tools.
2016-02-08 16:37:21 -05:00
## Removing volumes
A Docker data volume persists after a container is deleted. You can create named
or anonymous volumes. Named volumes have a specific source form outside the
container, for example `awesome:/bar` . Anonymous volumes have no specific
2016-07-10 00:26:58 -04:00
source. When the container is deleted, you should instruct the Docker Engine daemon
2016-02-08 16:37:21 -05:00
to clean up anonymous volumes. To do this, use the `--rm` option, for example:
```bash
2016-05-05 07:08:22 -04:00
$ docker run --rm -v /foo -v awesome:/bar busybox top
2016-02-08 16:37:21 -05:00
```
This command creates an anonymous `/foo` volume. When the container is removed,
2016-07-10 00:26:58 -04:00
the Docker Engine removes the `/foo` volume but not the `awesome` volume.
2016-02-08 16:37:21 -05:00
2015-09-16 13:09:10 -04:00
## Important tips on using shared volumes
2016-02-08 16:37:21 -05:00
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.
2015-09-16 13:09:10 -04:00
2016-02-08 16:37:21 -05:00
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.
2015-09-16 13:09:10 -04:00
2014-05-21 17:05:19 -04:00
# Next steps
2016-02-08 16:37:21 -05:00
Now you've learned a bit more about how to use Docker we're going to see how to
2014-05-21 17:05:19 -04:00
combine Docker with the services available on
2014-06-01 16:48:04 -04:00
[Docker Hub ](https://hub.docker.com ) including Automated Builds and private
2014-05-21 17:05:19 -04:00
repositories.
2016-06-13 14:08:11 -04:00
Go to [Store images in Docker Hub ](dockerrepos.md ).