Mastering Docker Storage Management: A Hands-On Demo

Welcome back to another enlightening Docker demo. In this session, we’re going to dive deep into the world of Docker storage management, creating volumes, working with Bind Mounts, and harnessing the power of tmpfs. Let’s jump right in and discover how to work with these storage features.

Creating Docker Volumes

To start, let’s create some Docker volumes to store and manage data. First, let’s list the existing volumes:

docker volume ls

It appears we have no volumes at the moment. Let’s create a few using the docker volume create command:

docker volume create mysql-vol-1
docker volume create mysql-vol-2
docker volume create mysql-vol-3

Now, if we list the volumes again:

docker volume ls

You can see that we have successfully created three Docker volumes.

Exploring Volume Structure

To get an idea of where these volumes are located on your system, we can navigate to the var/lib/docker/volumes directory. You might need root permissions for this, so use sudo su:

cd /var/lib/docker/volumes
ls

You’ll notice that there’s a folder for each volume you created. Within each volume’s folder, there’s a _data directory where the actual data is stored.

Working with Docker Volumes

Now, let’s create a container and use one of these volumes. We’ll write some data to the volume in this container, then create another container, mount the same volume, and verify if the data is still there.

We’ll start by creating a container:

docker container run -dit --name mysql1 --mount type=volume,source=mysql-vol-1,target=mysql-data ubuntu

This command creates a new container named mysql1 and mounts the mysql-vol-1 volume to the /mysql-data directory inside the container.

Now, let’s enter the container:

docker exec -it mysql1 bash

Inside the container, if you run df -h, you’ll see the mysql-data volume mounted. Let’s create a file in this directory to demonstrate persistence:

echo "$(date): Hostname of the container is $(hostname)" > /mysql-data/sample.txt

You can replace the message with anything you like.

Exit the container:

exit

Now, let’s stop and remove the mysql1 container:

docker container kill mysql1

Creating another container with the same name would cause an error because the old container is still in the inactive state. For this demo, we’ll name the new container mysql2. The process of removing the old container would be different in a real-world scenario where you might want to keep your data intact.

docker container run -dit --name mysql2 --mount type=volume,source=mysql-vol-1,target=mysql-data ubuntu

Enter the new container:

docker exec -it mysql2 bash

Check if the data is still available inside the mysql-data directory:

cat /mysql-data/sample.txt

You should see the data written by the previous container. This demonstrates the persistence of data stored in Docker volumes.

Using Bind Mounts

Next, let’s explore Bind Mounts. Bind Mounts allow you to map a path from your Docker host machine into a container. Here, we’ll mount a path from your local system into the container. We’ll create a new container and mount a local directory into it.

docker container run -dit --name myubuntu1 -v /home/training:/home-data ubuntu

This command mounts the /home/training directory on your host machine to the /home-data directory in the container named myubuntu1.

Enter the container:

docker exec -it myubuntu1 bash

Check if the mounted path is available:

cd /home-data/
ls

You should see the contents of your local directory inside the container.

Create a file within the container:

touch /home-data/created-from-container.txt

Exit the container:

exit

Back on your host machine, verify that the file is also created locally:

ls /home/training/

You should see the file created-from-container.txt in your local directory.

This demonstrates how Bind Mounts allow you to share data between your host machine and the container.

Leveraging tmpfs

Now, let’s explore tmpfs, a temporary file system created inside RAM. This is ideal for non-persistent data requiring high-speed access.

Create a container with a tmpfs mount:

docker container run -dit --name ubuntu3 --mount type=tmpfs,destination=/data/tmp ubuntu

Enter the container:

docker exec -it ubuntu3 bash

Check if the tmpfs mount is available:

df -h

You’ll see the /data/tmp mount with a file system type of tmpfs.

Exit the container:

exit

Let’s explore an alternative

way of creating a tmpfs mount using the --tmpfs option:

docker container run -dit --name ubuntu4 --tmpfs /data/tmp ubuntu

Enter the container:

docker exec -it ubuntu4 bash

Verify that the tmpfs mount is available:

df -h

You’ll again see the /data/tmp mount with a tmpfs file system.

This demonstrates how tmpfs mounts are created in memory, providing high-speed access for non-persistent data.

A Note on Volumes with tmpfs

It’s worth noting that if you create a volume mount using -v or --volume with a tmpfs option, it will actually create a volume and mount it inside the container, not as a tmpfs mount.

Conclusion

This hands-on demo has equipped you with essential knowledge for managing Docker storage. You’ve learned how to create Docker volumes, work with Bind Mounts to share data between your host machine and containers, and leverage tmpfs for high-speed, non-persistent data. These storage features provide you with the tools to manage your Docker containers efficiently and effectively. Happy Dockerizing!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top