working_with_volumes.rst 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. :title: Share Directories via Volumes
  2. :description: How to create and share volumes
  3. :keywords: Examples, Usage, volume, docker, documentation, examples
  4. .. _volume_def:
  5. Share Directories via Volumes
  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
  26. parameter ``-v`` can be used more than once in order to create more
  27. volumes within the new container. The example below shows the
  28. instruction to create a container with two new volumes::
  29. docker run -v /var/volume1 -v /var/volume2 shykes/couchdb
  30. For a Dockerfile, the VOLUME instruction will add one or more new
  31. volumes to any container created from the image::
  32. VOLUME ["/var/volume1", "/var/volume2"]
  33. Mount Volumes from an Existing Container:
  34. -----------------------------------------
  35. The command below creates a new container which is running as daemon
  36. ``-d`` and with one volume ``/var/lib/couchdb``::
  37. COUCH1=$(sudo docker run -d -v /var/lib/couchdb shykes/couchdb:2013-05-03)
  38. From the container id of that previous container ``$COUCH1`` it's
  39. possible to create new container sharing the same volume using the
  40. parameter ``-volumes-from container_id``::
  41. COUCH2=$(sudo docker run -d -volumes-from $COUCH1 shykes/couchdb:2013-05-03)
  42. Now, the second container has the all the information from the first volume.
  43. Mount a Host Directory as a Container Volume:
  44. ---------------------------------------------
  45. ::
  46. -v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro].
  47. If "host-dir" is missing, then docker creates a new volume.
  48. This is not available for a Dockerfile due the portability and sharing
  49. purpose of it. The [host-dir] volumes is something 100% host dependent
  50. and will break on any other machine.
  51. For example::
  52. sudo docker run -v /var/logs:/var/host_logs:ro shykes/couchdb:2013-05-03
  53. The command above mounts the host directory ``/var/logs`` into the
  54. container with read only permissions as ``/var/host_logs``.
  55. .. versionadded:: v0.5.0
  56. Known Issues
  57. ............
  58. * :issue:`2702`: "lxc-start: Permission denied - failed to mount"
  59. could indicate a permissions problem with AppArmor. Please see the
  60. issue for a workaround.