Data Persistence in Docker for Stateful Applications
- Kindly refer the following articles if you are not aware of layer
- By default, all the files created/modified by the containers reside on the writable layer of the container.
- The problem with the writable layer of the container is "Writable layer will be deleted once the container is deleted"
- To persist the data into the Docker Host (Machine on which Docker is installed) Docker provides the following options
- Volumes:
- Stored in the hostfile system managed by Docker (/var/lib/docker/volumes/ on Linux).
- Non Docker Processes should not modify the file system
- Bind Mounts:
- Can be stored any where on the host system.
- Non-Docker Processes can modify
- tmpfs Mounts:
- Store on the host systems memory, never written to file system

Docker Volumes
- Docker Volumes are preferred mechanisms for persisting data generated by and used by Docker Containers.
- Advantages
- Easier to backup or migrate that bind mounts
- Can be managed by Docker CLI or the API
- Work in both Linux & Windows Containers
- Volume Drivers lets to store volumes on remote hosts or cloud providers, encryption can be added.
- Volumes donot increase the size of the docker containers writable layer as the volumes’s contents exist outside of container lifecycle.
Creating and Managing Docker Volumes
- Two Options Exist for the Creation of Docker Vlume
- -v:
- Consists of three fields separated by :.
- fields order should be correnct
- syntax: __-v <name of volume>:<path to mount in container>:<options>
- Lets Experiment with volumes using -v and docker volume command
- Create a docker volume called as my-tools. Execute the following commands on docker host
docker volume create my-tools
docker volume ls
##Output##
DRIVER VOLUME NAME
local my-tools
docker volume inspect my-tools
##output##
[
{
"CreatedAt": "2019-10-03T13:21:12Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my-tools/_data",
"Name": "my-tools",
"Options": {},
"Scope": "local"
}
]
- Mount the docker volume to alpine container using -v command
docker container run --name cont1 -d -v my-tools:/tools alpine sleep 1d
- Execute inspect command & navigate to mount on container cont1 & execute other commands as shown below
docker container inspect cont1
##Output##
"Mounts": [
{
"Type": "volume",
"Name": "my-tools",
"Source": "/var/lib/docker/volumes/my-tools/_data",
"Destination": "/tools",
"Driver": "local",
"Mode": "z",
"RW": true,
"Propagation": ""
}
]
docker container exec cont1 touch /tools/1.txt
docker container exec cont1 touch /tools/2.txt
docker container exec cont1 touch /tools/3.txt
docker container exec cont1 ls /tools
##Output##
1.txt
2.txt
3.txt
- Lets mount the same volume on different container, in different path and see the contents of volume
docker container run --name cont2 -d -v my-tools:/mytools tomcat:8
docker container exec cont2 ls /mytools
##Output##
1.txt
2.txt
3.txt
- –mount:
- Initially was used only for docker services, now can be used for standalone containers as well
- Syntax: –mount ‘type=<bind/volume/tmpfs>,source/src=<nameofvolume>,destination/dst/target=<mountpath in container>,
- Refer Here for complete syntax
- Lets use the same my-tools volume using –mount
- Mount the docker volume to alpine container using –mount command
docker container run --name cont3 -d --mount 'type=volume,src=my-tools,dst=/tools' alpine sleep 1d
- Execute the rest of commands as mentioned above
Docker Volume Drivers
- Docker volume driver can be specified while creating a volume or when to start the container
- Refer Here for Docker Volume Drivers
- Refer Here for azure persistent volumes
- Refer Here for aws persistent data volume
References
Other Way of Creating Docker Volume
- In the Dockerfile use the VOLUME instruction.
Like this:
Like Loading...