August 12, 2017

Docker Volumes

We quickly go over how we use Volumes with our docker-compose.yml file to persist our database data.

We cover what our docker-compose.yml file is doing when we define and use a volume.

We cover these parts of the .yml file:

version: '2'
services:
  db:
    image: mysql:5.7
    ...
  # SPECIFICALLY THIS PART HERE  <-----
    volumes:
     - data:/var/lib/mysql
volumes:
  data:
    driver: "local"

After running docker-compose up -d, we get all of our artifacts created. One of these is a named volumed, which we've named data. We can inspect that volume:

$ docker volume ls
DRIVER              VOLUME NAME
local               phpapp_data

We can inspect the running containers using this volume as well to see it's mount points:

# Inspect container, pipe output to "jq" for nice json formatting
$ docker inspect phpapp_db_1 | jq

# Most info truncated, we wanted to see Mount data for this volume
[{
    ...
    "Mounts": [{
        "Name": "phpapp_data",
        "Source": "/var/lib/docker/volumes/phpapp_data/_data"
        ...
    }]
}]

In the video, we switch over to an Ubuntu server running Docker, since we can't view the contents of that data volume directly on the Macintosh here. We can't see that since Docker runs in a layer of virtualization on our Mac or Windows computers.

On the Ubuntu server, we spin up a similar MySQL container using a data volume configured in a docker-compose.yml file, anad then we inspect it to get the data directory. Finally we list out that directory to see the MySQL database data contents.

# On the Ubuntu server

# Find the Mount section to see the data volume mounted
$ docker inspect root_db_1

# List out the data volume contents on our Ubuntu server file system
$ ls -lah /var/lib/docker/volumes/root_data/_data
> auto.conf
> homestead
> ...other mysql database files

We can see this matches the data found in the directory /var/lib/mysql from inside the container as well.

# Start bash within the running mysql container
# So we are "logged in" (so to speak) and can investigate the container
$ docker exec -it root_db_1 bash

# List out the shared mysql directory and we'll see the same database contents
$ ls -lah /var/lib/mysql
> auto.conf
> homestead
> ...other mysql database files

Finally we spin down the MySQL docker container (destroying it!) and re-create it. After doing that, we can see that our MySQL database data has persisted - the volume was NOT destroyed when we ran destroyed the container.

# Destroy our mysql container
$ docker-compose down

# See our volume still exists
$ docker volume ls

# Restart the container
$ docker-compose up -d

# We'll see that our database(s) still exist!
$ docker exec -it root_db_1 bash
> mysql -u root -p -e "show databases"
#... Our databases get listed out

Looking for a deeper dive into Docker?

Sign up here to get a preview of the Shipping Docker course! Learn how to integrate Docker into your applications and develop a workflow to make using Docker a breeze!

All Topics