Predictive Hacks

Docker Storage Tutorial

docker

Docker has two main categories of data storage, the persistent and the non-persistent.

Persistent Data Storage

Persistent data storage are the volumes which are decoupled from the containers.

Volumes:

  • Use a volume for persistent data:
    • Create the volume first, then create your container.
  • Mounted to a directory in the container
  • Data is written to the volume
  • Deleting a container does not delete the volume
  • First-class citizens
  • Uses the local driver
  • Third party drivers:
    • Block storage
    • File storage
    • Object storage
  • Storage locations:
    • Linux: /var/lib/docker/volumes/
    • Windows: C:\ProgramData\Docker\volumes

Non-Persistent Data Storage

  • Non-persistent
    • Local storage
    • Data that is ephemeral
    • Every container has it
    • Tied to the lifecycle of the contain
  • By default all container use local storage
  • Storage locations:
    • Linux: /var/lib/docker/[STORAGE-DRIVER]/
    • Windows: C:\ProgramData\Docker\windowsfilter\
  • Storage Drivers:
    • RHEL uses overlay2.
    • Ubuntu uses overlay2 or aufs.
    • SUSE uses btrfs.
    • Windows uses its own.

Volume Commands

List all Docker volume commands:

docker volume -h
  • create: Create a volume.
  • inspect: Display detailed information on one or more volumes.
  • ls: List volumes.
  • prune: Remove all unused local volumes.
  • rm: Remove one or more volumes.

List all volumes:

docker volume ls

Create two new volumes:

docker volume create test-volume1
docker volume create test-volume2

Get the flags available when creating a volume:

docker volume create -h

Inspecting a volume:

docker volume inspect test-volume1

Deleting a volume:

docker volume rm test-volume

Removing all unused volumes:

docker volume prune

Bind Mounts

Bind mounts have been around since the early days of Docker. They have limited functionality compared to volumes. With bind mount, a file or directory on the host machine is mounted into a container.

Volumes use a new directory that is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.

Using the mount flag:

docker container run -d --name <NAME> --mount type=bind, source=<SOURCE>, target=<TARGET> <IMAGE>

Using the volume flag:

docker container run -d --name <NAME> -v <SOURCE>:<TARGET> <IMAGE>

Let’s provide a walk through example of how to run a container with volumes. We are in our working directory, and we create a folder called mysource which will be connected with our docker container under the mytarget folder, where in our case is the nginx. Let’s run the command (Note that I am on Windows that is the reason for the backslash in the source path):

docker container run -d --name nginx_with_volume -v  C:\ForTheBlog\VolumeExample\mysource:/mytarget nginx

Now, let’s say that we create a new file called myfile1.txt, in our local PC under the mysource folder:

Now, we expect to find the file1.txt within the container under the mytarget folder. Let’s run the bash command in an interactive terminal (-it):

docker exec -it nginx_with_volume bash

We’re inside the container. We can search for the files and directories with the ls command:

ls -ltr

We can change directory (cd) to mytarget:

cd mytarget
ls -ltr

As we can see the file1.txt mounted from our local PC inside the container. Finally, let’s create a new file called file2.txt within the container under the mytarget folder.

touch file2.txt
echo "A file created within the container" > file2.txt

We expect to find the file2.txt in our local PC under the mysource folder. As we can see it is there!

Remove the Container

Since we are done with this tutorial, it is time to clean our mess :). So, let’s stop and then remove the container.

docker stop nginx_with_volume
docker rm nginx_with_volume

Reference

[1] Linux Academy

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s