1

Description

Official Docker documentation are usually not very useful, and alot of times things remain unclear even after reading through their sections.

There are many things unclear, but this question I just want to target these:

When running docker volume create:

--driver --opt device --opt type

When I run docker volume create --driver local --opt device=:/var/www/html/app --opt type=volume volumename I actually do get a volume :

  $docker volume inspect customvolume`
  [
  {
    "CreatedAt": "2020-08-03T09:28:10Z",
    "Driver": "local",
    "Labels": {},
    "Mountpoint": "/var/lib/docker/volumes/customvolume/_data",
    "Name": "customvolume",
    "Options": {
        "device": ":/var/www/html/customfolder",
        "type": "volume"
    },
    "Scope": "local"
}

]

Trying to mount this new volume:

 docker run --name test-with-volume \ 
    --mount source=customvolume,target=/var/www/html/app77' \ 
    my-app-only:latest

Error:

     Error response from daemon: error while mounting
     volume '/var/lib/docker/volumes/customvolume/_data': failed to 
    mount local volume: mount     :/var/www/html/customfolder:/var/lib/docker/volumes/customvolume/_data: no such device.

Questions

Clearly the options allow you to do some unexpected things, I was able to create a volume volume at a custom location, but it is not mountable.

  • What are the options for type (with difference of each explained) : when using docker volume create, they are unclear to me. docker run --mount documentation talks about volume, bind, tmp, but on docker volume create they only show examples, which are tmpfs, btrfs, nfs.

  • When can you use device?
    I thought this could be used to create a custom location for volume type (aka named volumes) on the source host (similar to how bind-mounts can be mounted) I assumed I could use the 'recommended way of named volumes including a custom folder location' instead of host mounts (bind-mounts).

  • Finally, how could you setup a docker-compose.yml volume custom driver correctly as well.

I think the confusion lies in the fact that docker run --mount vs docker volume create seems to be inconsistent, because of how unclear Docker documentation quality is

3 Answers 3

7

There are two main categories of data — persistent and non-persistent.

Persistent is the data you need to keep. Things like; customer records, financial data, research results, audit logs, and even some types of application log data. Non-persistent is the data you don’t need to keep.

Both are important, and Docker has solutions for both. To deal with non-persistent data, every Docker container gets its own non-persistent storage. This is automatically created for every container and is tightly coupled to the lifecycle of the container. As a result, deleting the container will delete the storage and any data on it. To deal with persistent data, a container needs to store it in a volume. Volumes are separate objects that have their lifecycles decoupled from containers. This means you can create and manage volumes independently, and they’re not tied to the lifecycle of any container. Net result, you can delete a container that’s using a volume, and the volume won’t be deleted.

This writable layer of local storage is managed on every Docker host by a storage driver (not to be confused with a volume driver). If you’re running Docker in production on Linux, you’ll need to make sure you match the right storage driver with the Linux distribution on your Docker host. Use the following list as a guide:

  • Red Hat Enterprise Linux: Use the overlay2 driver with modern versions of RHEL running Docker 17.06 or higher. Use the devicemapper driver with older versions. This applies to Oracle Linux and other Red Hat related upstream and downstream distros.

  • Ubuntu: Use the overlay2 or aufs drivers. If you’re using a Linux 4.x kernel or higher you should go with overlay2.

  • SUSE Linux Enterprise Server: Use the btrfs storage driver.

  • Windows Windows only has one driver and it is configured by default.

By default, Docker creates new volumes with the built-in local driver. As the name suggests, volumes created with the local driver are only available to containers on the same node as the volume. You can use the -d flag to specify a different driver. Third-party volume drivers are available as plugins. These provide Docker with seamless access external storage systems such as cloud storage services and on-premises storage systems including SAN or NAS.

$ docker volume inspect myvol
[
{
"CreatedAt": "2020-05-02T17:44:34Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/myvol/_data",
"Name": "myvol",
"Options": {},
"Scope": "local"
}
]

Notice that the Driver and Scope are both local. This means the volume was created with the local driver and is only available to containers on this Docker host. The Mountpoint property tells us where in the Docker host’s filesystem the volume exists.

With bind mounts

version: '3.7'
services:
   maria_db:
    image: mariadb:10.4.13
    environment:
      MYSQL_ROOT_PASSWORD: Test123@123
      MYSQL_DATABASE: database
    ports:
      - 3306:3306
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./data_mariadb/:/var/lib/mysql/  

With volume mount

version: "3.8"
services:
  web:
    image: mariadb:10.4.13
    volumes:
      - type: volume
        source: dbdata
        target: /var/lib/mysql/ 

volumes:
  dbdata:  

Bind mounts explanation

Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its full or relative path on the host machine. By contrast, when you use a volume, a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.

tmpfs mounts explanation

Volumes and bind mounts let you share files between the host machine and container so that you can persist data even after the container is stopped. If you’re running Docker on Linux, you have a third option: tmpfs mounts. When you create a container with a tmpfs mount, the container can create files outside the container’s writable layer. As opposed to volumes and bind mounts, a tmpfs mount is temporary and only persisted in the host memory. When the container stops, the tmpfs mount is removed, and files are written there won’t be persisted.

Volume explanation

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.

6
  • Thank you for the detailed explanation, this will be really helpful to refer back to over time. However, my questions were specifically about --driver (somedrivertype), --opt device=:/path` and --opt type=volume for persistent custom locations managed by Docker (ie. named volume - not bind-mount). Examples shown here are are just for docker volume create which creates it in /var/lib/docker/volumes/volumename/_data folder. Aug 3, 2020 at 13:35
  • I think these examples discuss everything on the host /var/lib/docker/volumes hosted; for named volumes, please update me if I misunderstood slightly. May you give an example of how you would configure docker to user overlay2 or aufs type for Ubuntu for instance? (or just another OS type). My main objective was to take the 'recommended way by Docker`, but also have the opportunity to specify a custom location. I have since read up on plugins and I think I might have found a solution there. Documentation of Docker suggested it is possible natively (without plugins) Aug 3, 2020 at 13:42
  • 1
    you can't us named volume with a custom location. if you need to use a custom location you need to use a bind mount option. you can change your default storage driver by updating "/etc/docker/daemon.json" file. Aug 3, 2020 at 14:37
  • for an example ` udayanga@udayanga-PC:~$ cat /etc/docker/daemon.json { "storage-driver": "overlay2" } udayanga@udayanga-PC:~$ ` Aug 3, 2020 at 14:40
  • Interestingly I do not have /etc/docker/daemon.json. I think the topic discussed storage-drivers more, and I now assume that --driver local, nfs, tmpfs are for volumes, and the storage-driver is the OS storage type. Reading the answer again above : ` (not to be confused with a volume driver)` sort of confirms this. Problem mainly is that I do not find Docker documentation that clear... almost ever. Aug 4, 2020 at 7:53
3

Recently I searched for something similar: how to force a docker volume into writing its data to a custom path that is actually the mount point of a persistent disk. There were 2 motives:

  1. first avoid that the docker volume would be stuck inside the VM Image's disk space.
  2. second have the data outlive the docker volume itself (e.g. easy to reuse on another VM instance and freshly created docker volume).

This seemed feasible by passing extra options to the standard local driver when executing docker volume create. For example the command below makes the docker volume tmp-volume write into the device's argument value. Note that docker volume inspect still outputs a completely different but unused MountPoint. It worked when Ubuntu was the host OS inside that VM instance:

docker volume create -d local  --name tmp-volume\
    --opt device="/mnt/disks/disk-instance-test-volume" \
    --opt type="none" \
    --opt o="bind"

Maybe this is overlapping with your use-case? I blogged the whole story here in more detail: https://medium.com/@francis.meyvis/how-to-force-a-docker-volume-on-a-gce-disk-45b59d4973e?source=friends_link&sk=0e71ef39db84f4cb0ecccc7cd0f3c254

1
  • I couldn't get local to 'outlive (persist)' the containers; once I removed the containers, the data would be gone (I use docker-compose - so the volume gets removed as well). I ended up using local-persist as mentioned in my answer. This seems to either not support docker-compose volume removal which in my case is exactly what I need or because it is a plugin, docker-compose might think it better not to touch the volume. I will definitely try out your method for my next volume tests to see how it behaves compared to local-persist Aug 31, 2020 at 9:09
1

Damith's detailed explanation about named-volumes vs bind-mounts is a good reference to read for anyone. To answer the question I had, he talked about 3rd party plugins so I had to investigate further.

There seems to be no way to use custom location when using a named-volume (only the bind-mounts are able to) with a default Docker installation, but there is indeed a plugin that acts similarly to named-volumes but with some extra functionality.

While this only partially answers some of the things I mentioned in question (and still not clear about), use this for reference if you want to use named-volume acting like bind-mounts

Solution

For my particular use case, the Docker plugin local-persist seems to solve my requirements, it has the capability to 1) persist data when containers get deleted and 2) provide a way to use a custom location.

Matchbooklab Docker local-persist

Installation:

Confirmed to work with Ubuntu 20.04 installation

  • Run this install script: note: there is also custom installation instructions at the github link if you want to install it manually.

    curl -fsSL https://raw.githubusercontent.com/MatchbookLab/local-persist/master/scripts/install.sh | sudo bash
    

This will install and setup startup script for local-persist to monitor volumes.

Setup volume

  • Create a new local-persist volume:

    docker volume create -d local-persist --opt mountpoint=/custom/path/on/host --name new-volume-name
    

Usage

  • Attach the volume to a container:

Newer --mount syntax:

   docker run --name container-name --mount 'source=new-volume-name,target=/path/inside/container'

-v syntax: (not tested - as shown in github readme)

   docker run -d -v images:/path/inside/container/ imagename:version
  • Or with docker-compose.yml: (example shows v2; not tested yet)

    version: '2'
    
    services:
      one:
        image: alpine
        working_dir: /one/
        command: sleep 600
        volumes:
          - data:/one/
    
      two:
        image: alpine
        working_dir: /two/
        command: sleep 600
        volumes:
          - data:/two/
    
    volumes:
      data:
        driver: local-persist
        driver_opts:
          mountpoint: /data/local-persist/data
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.