Docker volumes

What are they, how to use it and how to perform a backup

Docker volumes

Why would I use a volume?

I would use a volumes where I want to manage data outside of my container.

If we use the example of a web server in a container, I could choose to include the content in image and have them all deployed together and all would be fine.

But if I needed to change that content, I would need to rebuild and redeploy the image. Not a complete disaster, especially if you wanted to version the content, but it is only the content that would be changing so why include the web server mechanics? Why not allow them to live their own seperate lifecycles?

We could do this with volumes by keeping the - infrequently changed - web server in the image and keeping the - frequently changed - web content on a volume.

The volume content can be backed up, changed, deleted etc. and the web server would just "pick it up".

Volumes also afford us the ability to move the content to remote storage and share it among other containers.

What type of docker volumes are there?

There are three types of volumes:

  • bind mounts
    • created to map host path to container path
  • named volumes
    • created using docker volume management
    • custom name
  • anonymous volumes
    • dynamically created but managed by docker volume management
    • random name
    • not easy to find hence why named volumes are the weapon of choice
  • temporary volume (tmpfs)
    • stores empheral data

The best practice as at time of writing is to use the -mount command as it is more explicit and easier to use

Where are docker volumes stored?

Lets create a volume and then list all volumes on my machine:

# create a named docker volume
docker volume create mydata
# list docker volumes
docker volume ls

We should see our new volume mydata in the list of volumes:

local               mydata

Now we can inspect the volume to see where it is located:

# inspect the volume
docker inspect mydata

should present something like:

        "CreatedAt": "2020-02-05T15:57:56Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/mydata/_data",
        "Name": "mydata",
        "Options": {},
        "Scope": "local"

We now see where the volume is mounted:


You can place data in and out of here but will need root access