Creating volumes

Since, at this time, when using Docker for Mac or Windows containers are not running natively on OS X or Windows but rather in a (hidden) VM created by Docker for Mac and Windows, it is best we use docker-machine to create and use an explicit VM running Docker. At this point, we assume that you have Docker Toolbox installed on your system. If not, then please go back to Chapter 2Setting up a Working Environment, where we provide detailed instructions on how to install Toolbox.

Use docker-machine to list all VMs currently running in VirtualBox:

$ docker-machine ls 

If you do not have a VM called node-1 listed then create one:

$ docker-machine create --driver virtualbox node-1 

If you have a VM called node-1 but it is not running then please start it:

$ docker-machine start node-1

Now that everything is ready, SSH into this VM called node-1:

$ docker-machine ssh node-1

You should be greeted by a boot2docker welcome image .

To create a new data volume, we can use the docker volume create command. This will create a named volume which can then be mounted into a container and be used for persistent data access or storage. The following command creates a volume, my-data using the default volume driver:

$ docker volume create my-data 

The default volume driver is the so-called local driver which stores the data locally in the host filesystem. The easiest way to find out where the data is stored on the host is by using the inspect command on the volume we just created. The actual location can differ from system to system and so, this is the safest way to find the target folder:

 $ docker volume inspect my-data 
[ 
    { 
        "CreatedAt": "2018-01-28T21:55:41Z", 
        "Driver": "local", 
        "Labels": {}, 
        "Mountpoint": "/mnt/sda1/var/lib/docker/volumes/my-data/_data", 
        "Name": "my-data", 
        "Options": {}, 
        "Scope": "local" 
    } 
] 

The host folder can be found in the output under Mountpoint. In our case, when using docker-machine with a LinuxKit-based VM running in VirtualBox, the folder is /mnt/sda1/var/lib/docker/volumes/my-data/_data.

The target folder often is a protected folder and we thus might need to use sudo to navigate to this folder and execute any operations in it. In our case, we do not need to use sudo:

$ cd /mnt/sda1/var/lib/docker/volumes/my-data/_data
If you are using Docker for Mac to create a volume on your laptop and then do a docker volume inspect on the volume you just created, the Mountpoint is shown as /var/lib/docker/volumes/my-data/_data. But you will discover that there is no such folder on the Mac. The reason is that the path is in relation to the hidden VM that Docker for Mac uses to run containers. At this time, containers cannot run natively on OS X. The same applies to volumes created with Docker for Windows.

There are other volume drivers available from third parties in the form of plugins. We can use the --driver parameter in the create command to select a different volume driver. Other volume drivers use different types of storage systems to back a volume, such as cloud storage, NFS drives, software-defined storage and more.