March 25, 2017

Mount Host Directory As Data Volume

Mount host directory as a Data Volume

Docker allows you to mount a directory from the Docker host into a container. Using the -v option, host directories can be mounted in two ways: using an existing host volume, e.g. /home/john/app01, or a new auto-generated volume on the host, e.g. /var/lib/docker/volumes/53404f432f0….
  • Mount an existing host volume in the container:
    • $ docker run -v /home/john/app01:/app01 -i -t busybox In this example, the -v parameters are:
      • /home/john/app01 -- Docker host volume
      • : -- a single colon
      • /data -- container path where the host volume will be mounted
    • Any existing files in the host volume (/home/john/app01) are automatically available in the container mount point, /app01
    • To maintain portability, you cannot map a host directory to a container via the Dockerfile, as this specific directory may not be available on another host where the Dockerfile is applied.
    • More generally:
      • $ docker run -v <host_dir>:<container_dir>:ro -i -t <image> <default executable>
        • is the source directory
        • is the container directory
        • Add :ro to make the mount read-only
    • In addition to directories, the -v option can be used to mount single files between the host and container
  • Mount a Docker created host volume in the container:
      • $ docker run -v /app02 -i -t busybox
    • Docker creates a volume and maps it to an internal path on the Docker host
    • Docker creates a new volume on the host,  e.g. /var/lib/docker/volumes/14d8613f61f3a977c0b71e585d72e1099234084ee683259157f576b51baa4f64/_data and maps it to a volume, e.g. /app02, in the container.
    • In this example, the -v parameter is:
      • /app02 -- container path where the host volume will be mounted
    • Changes made in the container volume are immediately reflected in the host volume and vice versa.
    • You can assign the volume a name using the --name option, otherwise Docker assigns it a 64-character volume identifier. Use the docker volume inspect command to find the Volume Identifier:
    • "Mounts": [
              "Type": "volume",
              "Name": "14d8613f61f3a977c0b71e585d72e1099234084ee683259157f576b51baa4f64",
              "Source": "/var/lib/docker/volumes/14d8613f61f3a977c0b71e585d72e1099234084ee683259157f576b51baa4f64/_data",
              "Destination": "/app02",
              "Driver": "local",
              "Mode": "",
              "RW": true,
              "Propagation": ""
    • The advantage of Docker created host volumes is portability between hosts. Using it you do not require a specific volume to be available on any host that will make the mount.
  • A container volume is available for other containers to mount using the --volumes-from option. The following figure and commands demonstrate volume sharing among three containers:
    • container frosty_babbage has a volume /app02
    • new container alpinecontainer mounts its volume
    • second new container, alpinecontainer02 mounts volume from alpinecontainer
  • Create a container with a container volume, /app02: $ docker run -v /app02 -it alpine sh.
  • Create a new container and use --volumes-from to mount volume from the above container:
    $ docker run -i -t --name alpinecontainer --volumes-from frosty_babbage alpine sh
    • alpinecontainer is created
    • volume(s) mounted from container frosty_babbage using the --volumes-from option(s)
    • Notice below that volume /app02 is available, having been mounted from container frosty_babbage (container ID 8864dadc7c68):
  • Start a third container, alpinecontainer01 to mount volumes from alpinecontainer:
  • $ docker run -i -t --name alpinecontainer01 --volumes-from alpinecontainer alpine sh
    • alpinecontainer01 is created
    • volume(s) mounted from alpinecontainer using the --volumes-from option
    • Notice below that volume /app02 is available, having been mounted from container alpinecontainer (container ID 696646d6f37e):
  • Multiple -v or --volumes-from options can be used to mount multiple data volumes
  • Volume Use Cases:
    • Improved performance as it bypasses the storage driver, e.g. AUFS
    • Sharing data between containers
    • Share data between the host and the container

No comments:

Post a Comment