June 12, 2018

Docker Volumes

^ Ad space to help offset hosting costs :D

We cover Docker volumes and make sure we have set our docker-compose.yml file correctly to use our created volumes.

List volumes:

docker volume ls

Create a volume named dbdata:

docker volume create dbdata

Then we can use this volume when we spin up a container, such as MySQL:

docker run --rm -d \
    --name=mysql \
    --network=appnet \
    -v dbdata:/var/lib/mysql \
    -e MYSQL_DATABASE=homestead \
    -e MYSQL_USER=homestead \
    -e MYSQL_USER_PASSWORD=secret \

Here we used the -v flag and shared our named volume dbdata and bound it to /var/lib/mysql within the container.

We can create and destroy MySQL containers as much as we want. As long as we share the dbdata volume, that database data will persist (assuming, of course, we don't delete the volume via docker volume rm dbdata)!

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!


Docker Volumes

All Topics