Docker Volumes, Mounting, and More

In Docker, whenever you create a container, there is a specified place where the data for the container will be stored. If you do not provide any location for that data to be stored, then it will be stored in the container. When you delete the container, the data will also be lost. However, when working on enterprise projects, to prevent data loss, you can remove a container but persist the data. You can use this data to create more containers and share the data between these containers. This is where Docker volume comes into play.

Features of Docker Volumes

  • Sharing volume data and storage among multiple containers and host filesystems.
  • Decoupling containers from storage.
  • Does not delete contained data when deleting the container.
  • Back up, restore, and migrate data easily.

In this tutorial, we will explain what a Docker volume is and how to use it, as well as how to mount a volume in Docker.

Requirements

  • A system running Linux with Docker installed.
  • A root password is configured.

Basic Syntax of Docker Volume

You can use the “docker volume” command to see all available options with volume:

docker volume

You should see the following output after entering the above command:

Usage:  docker volume COMMAND

Manage volumes

Commands:
  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

Run 'docker volume COMMAND --help' for more information on a command.

Create a Volume

To create a data volume, you will first need to create a data volume on the Docker host and attach the volume to the container.

You can create a Docker volume using the command “docker volume create.” For example, use the following command to create a new Docker volume named myvolume:

docker volume create myvolume

You should see the following output:

myvolume

You can list your existing volume using the following command:

docker volume ls

You should see the following output:

DRIVER VOLUME NAME
local c2d2815ba1a75fbfe5d0a4b69d94269e55ccbc138c7d2e19504e501f1dbe634f
local myvolume

If you want to see more information about volume, run the following command:

docker inspect myvolume

You should get the following output after running the above command:

[
    {
        "CreatedAt": "2020-09-12T04:51:31Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/myvolume/_data",
        "Name": "myvolume",
        "Options": {},
        "Scope": "local"
    }
]

Mount a Volume

At this point, you have one volume named myvolume. In this example, you will create a container and mount a myvolume to the container.

You can use the –mount option to mount the volume. The basic syntax to mount the volume to the container is shown below:

docker run --mount source=volume-name,destination=path-inside-container docker-images

For example, to create a Ubuntu container and mount the myvolume to the container, run the following command:

docker run -it --name=volumecontainer --mount source=myvolume,destination=/data ubuntu

This command will pull the Ubuntu image from the Docker Hub, start the container in interactive mode with the name volumecontainer, and mount the myvolume container to the /data inside the /data directory:

Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
54ee1f796a1e: Pull complete
f7bfea53ad12: Pull complete
46d371e02073: Pull complete
b66c17bbf772: Pull complete
Digest: sha256:31dfb10d52ce76c5ca0aa19d10b3e6424b830729e32a89a7c6eee2cda2be67a5
Status: Downloaded newer image for ubuntu:latest
root@06a477c4e444:/#

You can check the mounted volume with the following command:

root@06a477c4e444:/# ls

You should see the data directory in the following output:

bin boot data dev etc home lib lib32 lib64 libx32 media mnt
opt proc root run sbin srv sys tmp usr var

Now, we will create a sample file named file.txt inside /data directory with the following command:

echo "This is a test file!" > /data/file.txt

Exit from the container with the following command:

exit

Start the same container again with the following command:

docker container start volumecontainer

Next, attach the running container with the following command:

docker exec -it volumecontainer /bin/bash

Check whether your file.txt is persistent with the following:

cat data/file.txt

You should get the following output after entering the above command:

This is a test file!

Share Data Between Containers

You can also share data between multiple containers using Docker volume.

As you know, we have created a volume named myvolume a new container named volumecontainer using this volume. We have also created a file named file.txt inside the volume.

Now, we will create another container named volumecontainer1 with the same myvolume volume using the following command:

docker run -it --name=volumecontainer1 --mount source=myvolume,destination=/data ubuntu

Run the ls command, as shown below:

ls

You should see the data directory in the following output:

bin boot data dev etc home lib lib32 lib64 libx32
media mnt opt proc root run sbin srv sys tmp usr var

Run the following command the check the file.txt:

cat /data/file.txt

You should see the same content that you created in the previous container:

This is a test file!

Mount Directory as a Volume

You can use the directory located inside your Docker host system as a volume and mount it to the container. You can also use the -v option to achieve the same, as shown below:

docker run -v "directory_name":volume_name docker_image

Create a directory named /data inside the Docker host with the following command:

mkdir /Data

Next, create some files inside the /Data directory:

cd /Data
touch file1.txt file2.txt file3.txt

Create a new container using the /Data directory as a volume with the following command:

docker run -it --name=data1 -v /Data:/Data ubuntu

This command will create a new container and mount the volume with the name /Data.

Run the following command to check the content of the /Data directory:

ls -l /Data/

You should see all the files we have previously created in the host system, as shown below:

total 0
-rw-r--r-- 1 root root 0 Sep 12 05:41 file1.txt
-rw-r--r-- 1 root root 0 Sep 12 05:41 file2.txt
-rw-r--r-- 1 root root 0 Sep 12 05:41 file3.txt

Remove Docker Volume

You can easily delete or remove the volume using the following syntax:

docker volume rm volume-name

For example, to remove the volume named myvolume, run the following command:

docker volume rm myvolume

You should get the following error:

Error response from daemon: remove myvolume:
 volume is in use - [06a477c4e4444c0f815a1ec4a151a8
339bf53d5060c492af867bcaebe531dd5d, fd8d05a027a755f
1df004ccf62568b5d66989c2112115c8a652ddbc8eb960948]

This is because your volume is used by the container. So, you will need to stop and remove the container before removing the volume.

To stop and remove the container, run the following command:

docker container stop volumecontainer volumecontainer1
docker container rm volumecontainer volumecontainer1

Now, you can easily remove the volume.

Conclusion

In the above guide, you learned what Docker volume is, how to create it, and how to mount it inside the container. You also learned how to share data among multiple containers using the volume.



from Linux Hint https://ift.tt/2SnRQTL

Post a Comment

0 Comments