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:
You should see the following output after entering the above 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:
You should see the following output:
You can list your existing volume using the following command:
You should see the following output:
local c2d2815ba1a75fbfe5d0a4b69d94269e55ccbc138c7d2e19504e501f1dbe634f
local myvolume
If you want to see more information about volume, run the following command:
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:
For example, to create a Ubuntu container and mount the myvolume to the container, run the following command:
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:
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:
You should see the data directory in the following output:
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:
Exit from the container with the following command:
Start the same container again with the following command:
Next, attach the running container with the following command:
Check whether your file.txt is persistent with the following:
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:
Run the ls command, as shown below:
You should see the data directory in the following output:
media mnt opt proc root run sbin srv sys tmp usr var
Run the following command the check the 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:
Create a directory named /data inside the Docker host with the following command:
Next, create some files inside the /Data directory:
touch file1.txt file2.txt file3.txt
Create a new container using the /Data directory as a volume with the following command:
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:
You should see all the files we have previously created in the host system, as shown below:
-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:
For example, to remove the volume named myvolume, run the following command:
You should get the following error:
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 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
0 Comments