We cover Docker volumes and make sure we have set our
docker-compose.yml file correctly to use our created volumes.
docker volume ls
Create a volume named
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_ROOT_PASSWORD=root \ -e MYSQL_DATABASE=homestead \ -e MYSQL_USER=homestead \ -e MYSQL_USER_PASSWORD=secret \ mysql:5.7
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)!