March 25, 2017

Container Data Volumes

Container Data Volumes

Docker data volumes allow data to:
  • persist after the container is exited, removed or deleted
  • be shared between the host and the Docker container
  • be shared with other Docker containers
It allows directories of the host system, managed by Docker, to be mounted by one or more containers. It's simple to setup as you don't need to pick a specific directory on the host system
  • A use-case for Volumes is as a way to share directories across containers
  • Simple example:
    • $ docker run --rm -i -t -v /data/vol01 debian bash
      • This creates a volume /data/vol01 and makes it available to the container
      • The container volume, /data/vol01, maps to a directory on the host file system. You can get the location via the $ docker inspect command. Look in the Mount section for the Source name/value pair:

      "Mounts": [
          {
              "Type": "volume",
              "Name": "dd517d905c98c74dc0c10370a46dd8445d67dbf84162dc0d9076b4040c395134",
              "Source": "/var/lib/docker/volumes/dd517d905c98c74dc0c10370a46dd8445d67dbf84162dc0d9076b4040c395134/_data",
              "Destination": "/data/vol01",
              "Driver": "local",
              "Mode": "",
              "RW": true,
              "Propagation": ""
          }
      ],
    • File(s) generated in the container (e.g. fileFromContainer.txt) is accessible from the "Source" directory on the host
  • A container with a volume mount can be run in "detached" mode and its volume(s) can be used by other containers as their data volume(s), e.g.: $ docker run -d -v debian bash
    • Another container can attach to this volume, /var/vol02, using the --volumes-from option and the name or ID of the container: $ docker run -it --volumes-from debian bash
    • Every container that mounts the volume from this container will have the same view of any files in the target directory.
    • Data volumes provide better and more predictable performance than the Union File System as they:
      • exists outside of the Union File System
      • bypass the storage driver and operate at native host speeds
      • do not incur potential overheads introduced by copy-on-write
  • Another example of creating data volumes:
    • Use docker run with one or more -v flags to add data volume(s) to a container:
      $ docker run -it --name testvol -h -v /data debian /bin/bash
      This will make the directory /data44 inside the container live outside the Union File System and directly accessible on the host. Any files that the image held inside the /data44 directory will be copied into the volume.
      • --rm - for short-lived containers. Container automatically removed when it exits
      • -t - Allocate a pseudo-TTY
      • -i - enable the STDIN stream, e.g. for keyboard interaction with container
      •  -h - Container host name
      • --name - Assign a name to the container, by default Docker assigns a pseudo-random friendly name, e.g. "serene_galileo"
      • -v  - container volume mount name

No comments:

Post a Comment