What is Docker volume and how to work with it?
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
What are Docker Volumes?
Docker volumes are file systems mounted on Docker containers to preserve data generated by the running container.
The volumes are stored on the host, independent of the container life cycle. This allows users to back up data and share file systems between containers easily.
There are different ways to mount a Docker volume while launching a container. Users can decide between the -v
and the --mount
flags, which are added to the docker run
command.
Mounting a Data Volume
To run a container and mount a data volume to it, follow the basic syntax:
docker run --mount source=[volume_name],destination=[path_in_container] [docker_image]
Mounting a Host Directory as a Data volume
You can mount host volumes by using the -v
flag and specifying the name of the host directory.
Everything within the host directory is then available in the container. What’s more, all the data generated inside the container and placed in the data volume is safely stored on the host directory.
The basic syntax for mounting a host directory is:
docker run -v "$(pwd)":[volume_name] [docker_image]
The "$(pwd)"
attribute instructs Docker to mount the directory the user is currently in.