IT 3300 : Virtualization

Docker Storage

Docker Storage

Remember that:

  • Data doesn't persist when a container no longer exists
  • It can be difficult to get data out of a container if another process needs it

Docker Storage

Options for storing files in the host machine... so that containers can access

  • For dev, you might use bind mounts
  • For prod, you should use volumes

Docker Storage Type Differences

  • Volumes are managed by docker (and placed into /var/lib/docker/volumes on Linux). This is the best option for making data persist.
  • Bind mounts are stored anywhere on the host system
  • tmpfs mounts are stored in memory only (RAM) never written to the host filesystem

Docker tmpfs

If your container generates non-persistent state data, consider using a tmpfs mount to avoid storing the data anywhere permanently, and to increase the container’s performance by avoiding writing into the container’s writable layer.

  • Cannot share data among containers
  • Good for sensitive files that you don't want to persist on the host or the container writable layer.

Docker tmpfs example

    docker run -d \
      -it \
      --name tmptest \
      --tmpfs /app \
      nginx:latest

Docker bind mount

  • Limited compared to volumes
  • You are mounting a file or dir on host machine into the container
    • Always use absolute path when referring to path on host
  • You have to manage the directory (ie. delete it when no longer used)
  • If bind mount directory doesn't exist, it will be created

Docker bind example

In the first, target must exist. The second will autocreate it.

    docker run -d \
      -it \
      --name devtest \
      --mount type=bind,source="$(pwd)"/target,target=/app \
      nginx:latest

     docker run -d \
      -it \
      --name devtest \
      -v "$(pwd)"/target:/app \
      nginx:latest

Docker volumes

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure of the host machine, volumes are completely managed by Docker.

When you create a volume, it is stored within a directory on the Docker host. When you mount the volume into a container, this directory is what is mounted into the container. This is similar to the way that bind mounts work, except that volumes are managed by Docker and are isolated from the core functionality of the host machine.

Docker volume advantages

  • Volumes are easier to back up or migrate than bind mounts.
  • You can manage volumes using Docker CLI commands or the Docker API.
  • Volumes work on both Linux and Windows containers.
  • Volumes can be more safely shared among multiple containers simultaneously
  • Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality
  • You can give a volume a name, or docker will assign one automatically (much like a container).

Docker volume use cases

  • Sharing data among multiple running containers. Volumes are only removed when you explicitly remove them.
  • When the docker host is not guaranteed to have a specific directory or file structure (i.e. Different versions of Linux might have different FS trees)
  • When you want to store on a cloud provider rather than locally
  • When you need to back up easily

Docker Volumes

  • Commands
    • docker volume create foo-vol
    • docker volume inspect foo-vol
    • docker volume rm foo-vol
    • docker volume ls

Start container with a volume

If you start a container with a volume that does not yet exist, Docker creates the volume for you.

    docker run -d \
      --name devtest \
      --mount source=myvol2,target=/app \
      nginx:latest
  • Then use docker inspect devtest to see the Mounts section.
  • See it in /var/lib/docker/volumes/myvol2.

Start container with a volume another example

This does the same as the last slide but with the -v instead of --mount.

    docker run -d \
      --name devtest \
      -v myvol2:/app \
      nginx:latest

Start container with a volume another example

Create a few of these:

  • docker run -dit --name sample3 --mount source=testvol,target=/foo debian /bin/bash
  • Create some files in /foo
  • See that you can also view them on any new container that you create

Tidbits

  • You can mount volumes read only
  • Remove all unused volumes and free up space: docker volume prune