working_with_volumes.rst 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. :title: Working with Volumes
  2. :description: How to create and share volumes
  3. :keywords: Examples, Usage, volume, docker, documentation, examples
  4. .. _volume_def:
  5. Data Volume
  6. ===========
  7. .. versionadded:: v0.3.0
  8. Data volumes have been available since version 1 of the
  9. :doc:`../api/docker_remote_api`
  10. A *data volume* is a specially-designated directory within one or more
  11. containers that bypasses the :ref:`ufs_def` to provide several useful
  12. features for persistant or shared data:
  13. * **Data volumes can be shared and reused between containers.** This
  14. is the feature that makes data volumes so powerful. You can use it
  15. for anything from hot database upgrades to custom backup or
  16. replication tools. See the example below.
  17. * **Changes to a data volume are made directly**, without the overhead
  18. of a copy-on-write mechanism. This is good for very large files.
  19. * **Changes to a data volume will not be included at the next commit**
  20. because they are not recorded as regular filesystem changes in the
  21. top layer of the :ref:`ufs_def`
  22. Each container can have zero or more data volumes.
  23. Getting Started
  24. ...............
  25. 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::
  26. docker run -v /var/volume1 -v /var/volume2 shykes/couchdb
  27. For a Dockerfile, the VOLUME instruction will add one or more new volumes to any container created from the image::
  28. VOLUME ["/var/volume1", "/var/volume2"]
  29. Create a new container using existing volumes from an existing container:
  30. ---------------------------------------------------------------------------
  31. The command below creates a new container which is running as daemon ``-d`` and with one volume ``/var/lib/couchdb``::
  32. COUCH1=$(sudo docker run -d -v /var/lib/couchdb shykes/couchdb:2013-05-03)
  33. From the container id of that previous container ``$COUCH1`` it's possible to create new container sharing the same volume using the parameter ``-volumes-from container_id``::
  34. COUCH2=$(sudo docker run -d -volumes-from $COUCH1 shykes/couchdb:2013-05-03)
  35. Now, the second container has the all the information from the first volume.
  36. Create a new container which mounts a host directory into it:
  37. -------------------------------------------------------------
  38. -v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro].
  39. If "host-dir" is missing, then docker creates a new volume.
  40. 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.
  41. For example::
  42. sudo docker run -v /var/logs:/var/host_logs:ro shykes/couchdb:2013-05-03
  43. The command above mounts the host directory ``/var/logs`` into the container with read only permissions as ``/var/host_logs``.
  44. .. versionadded:: v0.5.0