Search code examples
dockerdocker-composevolumes

Docker volumes newbie questions


For a service I've defined a volume as (an extract of my yml file)

services:
   wordpress:
      volumes:
         - wp_data:/var/www/html
      networks:
            - wpsite
networks: 
    wpsite:
volumes:
   wp_data:
      driver: local

I'm aware on a Windows 10 filesystem that the WP volumes won't be readily visible to me as they'll exist within the linux VM. Alternatively I'd have to provide a path argument to be able view my WP installation e.g.

volumes:
   - ./mysql:/var/lib/mysql

But my question is what is the point of the 'driver: local' option, is this default. I've tried with & without this option and can't see the difference.

Secondly what does this do? In my yml file I've commented it out to no ill effect that I can see!?

networks: 
    wpsite:

Solution

  • First question:
    The --driver or -d option defaults to local. driver: local is redundant. On Windows, the local driver does not support any options. If you were running docker on a Linux machine, you would've had some options: Official documentation here - https://docs.docker.com/engine/reference/commandline/volume_create/

    Second question:
    In each section networks:/volumes:/services: you basically declare the resources you need for your deployment.
    In other words, creating an analogy with a virtual machine, you can think about it like this: you need to create a virtual disk named wp_data and a virtual network named wpsite.
    Then, you want your wordpress service, to mount the the wp_data disk under /var/www/html and to connect to the wpsite subnet.

    You can use the following docker commands to display the resources that are created behind the scenes by your compose file:
    docker ps - show containers
    docker volume ls - show docker volumes
    docker network ls - show docker networks

    Hint: once you created a network or a volume, unless you manually delete it, it will not be destroyed automatically. You can clean-up manually the resources and experiment yourself by removing/adding more resources from your compose file.


    Updated to answer question in comment:
    If you run your docker on a Windows host, you probably enabled hyper-v. This allowed Windows to create a Linux VM, on top of which your docker engine is running.
    With the docker engine installed, docker can then create "virtual resources" such as virtual networks, virtual disks(volumes), containers(people often compare containers to VMs), services, containers etc.

    Let's look at the following section from your compose file:

    volumes:
       wp_data:
          driver: local
    

    This will create a virtual disk managed by docker, named wp_data. The volume is not created directly on your Windows host file system, but instead it is being created inside the Linux VM that is running on top of the HyperV that is running on your Windows host. If you want to know precisely where, you can either execute docker inspect <containerID> and look for the mounts that you have on that container, or docker volume ls then docker volume inspect <volumeID> and look for the key "Mountpoint" to get the actual location.