Skip to content

Instantly share code, notes, and snippets.

@rogaha
Last active December 26, 2015 04:29
Show Gist options
  • Save rogaha/7094090 to your computer and use it in GitHub Desktop.
Save rogaha/7094090 to your computer and use it in GitHub Desktop.
title

Working with Volumes

description

How to create and share volumes

keywords

Examples, Usage, volume, docker, documentation, examples

Data Volume

v0.3.0 Data volumes have been available since version 1 of the ../api/docker_remote_api

A data volume is a specially-designated directory within one or more containers that bypasses the ufs_def to provide several useful features for persistant or shared data:

  • Data volumes can be shared and reused between containers. This is the feature that makes data volumes so powerful. You can use it for anything from hot database upgrades to custom backup or replication tools. See the example below.
  • Changes to a data volume are made directly, without the overhead of a copy-on-write mechanism. This is good for very large files.
  • Changes to a data volume will not be included at the next commit because they are not recorded as regular filesystem changes in the top layer of the ufs_def

Each container can have zero or more data volumes.

Getting Started

Using data volumes is as simple as adding a new flag: -v. The parameter -v can be used more than once in order to create more volumes within the new container. The example below shows the instruction to create a container with two new volumes:

docker run -v /var/volume1 -v /var/volume2 shykes/couchdb

For a Dockerfile, the VOLUME instruction will add one or more new volumes to any container created from the image:

VOLUME ["/var/volume1", "/var/volume2"]

Create a new container using existing volumes from an existing container:

The command below creates a new container which is runnning as daemon -d and with one volume /var/lib/couchdb:

COUCH1=$(sudo docker run -d -v /var/lib/couchdb shykes/couchdb:2013-05-03)

From the container id of that previous container $COUCH1 it's possible to create new container using a copy of that volume using the parameter -volumes-from container_id:

COUCH2=$(sudo docker run -d -volumes-from $COUCH1 shykes/couchdb:2013-05-03)

Now, the second container has the all the information from the first volume.

Create a new container which mounts a host directory into it:

-v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro]. If "host-dir" is missing, then docker creates a new volume.

This is not available for a Dockerfile due the portability and sharing purpose of it. The [host-dir] volumes is something 100% host dependent and will break on any other machine.

For example:

sudo docker run -v /var/logs:/var/host_logs:ro shykes/couchdb:2013-05-03

The command above mounts the host directory /var/logs into the container with read only permissions as /var/host_logs.

v0.5.0

Volumes & persistent data storage

Current status

Docker has volumes which can be reused from container to container. These volumes have the following limitations:

  • they can't be imported and exported (without piping data into and out of the container)
  • they can't be backed up and restored from backups (without piping data into and out of the container)
  • they can't be stored on custom storage (e.g.: use high IOPS storage for a volume and regular IOPS storage for others)
  • it's not possible to cherry pick the volumes to be used from an old container in a new container
  • it's not possible to manage the volumes after deleting the containers to which they were attached
@rogaha
Copy link
Author

rogaha commented Oct 22, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment