Storage To keep data for the container is called container's storage. Types of Storage Non Persistent In this type of st...
Storage
To keep data for the container is called container's storage.
Types of Storage
Non Persistent
In this type of storage, the data will be lost if the container is deleted.
tmpfs
In this file system, the data will be stored in memory and it is only available during the container's lifetime, which means if the container is stopped or deleted the data will be lost. It is more suitable for the in-memory calculation. It is only for Linux OS.
Persistent
In this type of storage, the data will be persisted even though the container gets deleted.
Docker Volume
It is the storage that is maintained by the docker daemon in the docker area.. The default storage location is /var/lib/docker/volumes folder
Commands:
docker volume --help
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
Bind Mount
It is the storage that is managed by the admin of the system. It is a file or directory which is maintained on the host machine. It is having fewer features as compared to docker volume and it does not have any formal commands to manage these volumes by docker.You can use -v or --mount option to mount a directory of your host machine to the container
Example of Non-Persistent Storage.
Example1
Step 1: Create a container using ubuntu docker image
docker container run -it --name tmpcontainer -d ubuntu
Step 2: Go inside the container
docker exec -it tmpcontainer bash
Step 3: In this container, create a directory call test and store some files into it.
- mkdir test
- cd test
- touch file1 file2 file3 file4
- ls
Step 4: Stop the container.
docker stop tmpcontainer
Step 5: Start the container and check the test director still persist with all its files.
- docker start tmpcontainer
- docker exec -it tmpcontainer bash
- ls test
Step 6: Delete the container and think is there any way to get your test directory again.
docker rm -f tmpcontainer
No there is no way to get back the test directory data, because the container is deleted so storage in this container was non-persistent.
Example 2 (tmpfs)
Step 1: Create a container using ubuntu docker image
docker container run -it --name tmpcontainer --mount type=tmpfs,destination=/test -d ubuntu
Step 2: Go inside the container
docker exec -it tmpcontainer bash
Step 3: In this container, create a directory call test and store some files into it.
- cd test
- touch file1 file2 file3 file4
- ls
Step 4: Stop the container.
docker stop tmpcontainer
Step 5: Start the container and check the test director still persist with all its files or not. The files should be removed means you will not get any data inside test folder..
- docker start tmpcontainer
- docker exec -it tmpcontainer bash
- ls test
Step 6: Delete the container and think is there any way to get your test directory again.
docker rm -f tmpcontainer
No there is no way to get back the test directory data because the container is deleted so storage in this container was non-persistent.
Example 3: (tmps with tmpfs-mode)
Create a container with tmpfs and change the permission of destination folder.
docker container run -it --name c1 --mount type=tmpfs,destination=/tmp1,tmpfs-mode=1700 -d ubuntu
Persistence Storage
Example 1 (docker volume)
Step 0:- Delete all unused docker volumes
docker volume prune
Step 1:- To create a docker volume (demo-vol)
docker volume create demo-vol
Step 2:- List all docker volumes
docker volume ls
Step 3:- By default the driver is local and path for this docker volume is /var/lib/docker/volume.
ls /var/lib/docker/volumes/demo-vol/_data
Step 4:- Run a container which points its /app directory to demo-vol
docker container run -it --name c1 --mount source=demo-vol,destination=/app -d ubuntu
Step 5:- Go inside the container
docker exec -it c1 bash
Step 6:- Create some files under /app directory
cd app
touch file1 file2 file3 file4
exit
Step 7: Check these files are available under demo-volume's directory
ls /var/lib/docker/volumes/demo-vol/_data
Step 8:- Delete file4 from _data directory and check in c1 container's /app directory, file4 should not be available under this directory.
- rm /var/lib/docker/volumes/demo-vol/_data/file4
- docker exec -it c1 bash
- ls /app
Step 9:- Add a new file file5 in-app directory of the container and check this file should be available in demo-volume's data directory.
cd app
touch file5
exit
ls /var/lib/docker/volumes/demo-vol/_data
Step 10:- Delete container c1.
docker rm -f c1
Step 11:- Make sure your demo-vol data is not deleted.
ls /var/lib/docker/volumes/demo-vol/_data
Step 12:- Create a new container and attach demo-vol to that container's /demo directory.
docker container run -it --name c2 --mount source=demo-vol,destination=/demo -d centos
Step 13: Check in the container's demo directory whether all these files exist or not.
docker exec -it c2 bash
ls demo
Step 14: Delete the volume. Deletion of volume will delete the data as well.
docker rm -f c2
docker volume rm demo-vol
Note: You can use -v option instead of --mount with docker volume to mount a volume eg.
docker container run -it --name c3 -v demo-vol:/demo -d centos
Note: You can also refer to an existing directory of containers that points to docker volume.
docker container run -it --name c3 -v demo-vol:/root -d centos
In the above example, root directory's all files will be stored under demo-vol's data directory.
Example (Bind Mount)
Step 1: Create a directory (/home/vagrant/myfiles) that you want to map with the container's target directory
mkdir /home/vagrant/myfiles
Step 2:- Run a container which maps myfiles directory to container's /app1 directory.
docker container run -it --name bindmountcontainer -v /home/vagrant/myfiles:/app1 -d ubuntu
OR
docker container run -it --name bindmountcontainer --mount --type=bind,source=/home/vagrant/myfiles,target=/app1 -d ubuntu
Step 3:- Go inside the container and add some files in app1 directory
docker exec -it bindmountcontainer bash
cd app1
touch file1 file2 file3 file4
exit
Step 4:- Go to myfiles directory and find all the files that exist in this folder or not.
ls myfiles
Experiments to be performed with respect to docker volume and bind mount.
1. Attach a docker volume to multiple containers and add or modify the files in any of these containers and check that the changes are getting reflected in all the containers or not.
2. Point #1 is to be repeated for Bind Mount
3. Delete the Bind mount directory which is mapped to a container's destination directory and check the destination behavior means try to create some file or list the files on the destination folder.
4. Continue to Point 3, you may get some error that the source directory does not exist. Create source directory again and check the destination folder and find out whether you are able to create new files in this folder or not.
5. Restart the container which you are using for #3 and #4 and again check the destination folder to store some data and find out whether you are able to see it in the source folder or not.
UseCases
When you decide that you want your storage or persisting layer to be fully managed by Docker and accessed by docker containers only then you should go with docker volume.
If you need full control of the storage and plan on allowing other processes besides Docker to access or modify the storage layer, then bind mount is the righ choice.
COMMENTS