Get Started with Volumes

In this lab we learn how to create a basic docker volume and mount it in a container

Volume mounted in a container

  1. Execute the following command

    $ docker run -v /volume1 -i -t ubuntu /bin/bash
    

    This will mount /volume1 inside the container created from ubuntu image and launch and bash shell.

  2. The command above will get us into the terminal tty of the container. Run ls command and you will notice volume 1 there

    $root@b979b5d735b0:/# ls
    bin  boot  dev  etc  home  lib  lib64..  var  volume1
    
  3. On inspecting the docker container you will notice that /volume1 is mounted under / and is a ReadWrite Volume

    {
         "Volumes": {
        "/volume1": "/var/lib/docker/vfs/dir/012..5a569b392"
        },
        "VolumesRW": {
        "/volume1": true
        }
     }
    

Mount a Host Directory as a Volume

  1. Create a local directory /host/logs

    $ sudo mkdir -p /host/logs
    $ sudo touch /host/logs/log1.txt
    
  2. Execute the following command to bind /host/logs host directory to /container/logs volume

    $ docker run -v /host/logs:/container/logs -i -t ubuntu  /bin/bash
    
  3. Execute ls to see the contents of mounted volume. Notice that contents are the same as created in host directory.

    root@3774005dee32:/# ls
    bin  boot  container  .. srv  sys  tmp  usr  var
    root@3774005dee32:/# ls container/logs/
    log1.txt