[Music]
so in this video we're gonna learn about
docker volumes in a natural docker
volumes are used for data persistence in
docker so for example if you have
databases or other stateful applications
you would want to use docker volumes for
that so what are the specific use cases
when you need docker volumes so
container runs on a host let's say we
have a database container and a
container has a virtual file system
where the data is usually stored but
here there is no persistence so if I
were to remove the container or stop it
and restart the container then the data
in this virtual file system is gone and
it starts from a fresh state which is
obviously not very practical because I
want to save the changes that my
application is making in the database
and that's where I need docker volumes
so what are the docker volumes exactly
so on a host we have a physical file
system right and the way volumes work is
that we plug the physical file system
path it could be a folder a directory
and we plug it into the containers file
system path so in simple terms a
directory a folder on a host file system
is mounted into a directory or folder in
the virtual file system of docker so
what happens is that when it container
rights to its file system it gets
replicated or automatically written on
the host file system directory and
vice-versa so if I were to change
something on the host file system it
automatically appears in the container
as well so that's why when a container
restarts even if it starts from a fresh
state in its own virtual file system it
gets the data automatically from that
from the host because the data is still
there and that's how data is populated
on the startup of a container every time
you restart now there are different
types of docker volumes and so different
ways of creating them
usually the way to create docker volumes
is using the
kourin command so in the docker run
there is an option called - V and this
is where we define the connection of the
reference between the host directory and
the container directory and this type of
volume definition is called a host
volume and the main characteristic of
this one is that you decide where on the
host file system that reference is made
so which folder on the host file system
you mount into the container so the
second type is where you create a volume
just by referencing the container
directory so you don't specify which
directory on the host should be mounted
but that's taking care of the docker
itself so that directory is first of all
automatically created by docker under
the VAR lip docker
volumes so for each container there will
be a folder generated that gets mounted
automatically to the container and this
type of volumes are called anonymous
volumes because you don't have a
reference to this automatically
generated folder basically just have to
know the path and the third volume type
is actually an improvement of the
anonymous volumes and it specifies the
name of that folder on the host file
system and the name is up to you it just
to reference the directory and that type
of volumes are called named volumes so
in this case compared to anonymous
volumes you have you can actually
reference that volume just by name so
you don't have to know exactly the path
so from these three types the mostly
used one and the one that you should be
using in production is actually the
named volumes because they are
additional benefits to letting docker
actually manage those volume directories
on the host
now they showed how to create docker
volumes using docker run commands but if
you're using docker compose it's
actually the same so this actually shows
how to use volume definitions in a
docker compose and this is pretty much
the same as in docker run commands so we
have volumes at
buuuut and underneath you define your
volume definition just like you would in
this - B option and here we use a named
volume so DB - data will be the name
reference name that you can just think
of it could be anything and inviolable
data is the path in the container then
you may have some other containers and
at the end so in the same level as the
services you would actually list all the
volumes that you have defined you define
at least volumes that you want to mount
into the containers so if you were to
create volumes for different containers
you would list them all here and on the
container level then you actually define
under which path that specific volume
can be mounted and the benefit of that
is that you can actually mount a
reference of the same folder on a host
to more than one containers and that
would be beneficial if those containers
need to share the data in this case you
would mount the same volume name or
reference to two different containers
and you can mount them in two different
paths inside of the container thanks for
watching the video I hope it was helpful
and if it was don't forget to like it if
you want to be notified whenever a new
video comes out then subscribe to my
channel if you have any questions if
something wasn't clear in the video
please post them in the comment section
below and I will try to answer them so
thank you and see you in the next video