So by now we talked about volumes a lot.
I hope it's clear why we use them
and when you use which volume type
or when you use a bind mount.
Now I did say that volumes are managed by Docker.
And this is true.
When we run our container, as we did before,
with all these dash V flags here,
Docker we'll actually go ahead and create that volume,
which also means that it creates a folder
somewhere on the host machine automatically.
It does this without us giving any extra instructions.
Nonetheless, you can take a look under the hood
and see what Docker actually did.
Now here, I have my containers still up and running,
and now if I type docker volume dash dash help,
we'll see a couple of commands we can use
together with docker volume, to have a look
at all the volumes Docker's managing for all our containers.
In this case, we have only one container
but of course we could have multiple containers
and then we could view all the volumes by all containers.
So here we got various commands.
And right now the LS command is interesting.
It lists all the currently active volumes.
And if I run this, we see that we got free volumes here.
Two anonymous volumes, which you can tell
by this super long cryptic name and one named volume.
And that makes a lot of sense
if we have a look at our docker run instruction.
There, we have one named volume,
this one here.
And we did assign a name of feedback,
which is why we see this name here.
And then we have the anonymous volume for app node modules
and the anonymous volume for app temp.
So two anonymous volumes.
The bind mount does not show up in this list
because the bind mount is not a volume managed by Docker.
As I said and mentioned earlier, this binds a local folder
which we know, to a folder inside of the container.
And of course therefore Docker does not manage this.
It's our known folder on our host machine,
Docker is not in control over that.
But the other volumes are managed by Docker
And managed by Docker also means
that Docker will create this volume
if it doesn't exist yet, when you run a container.
You actually can also use docker volume create
to create a volume on your own.
If we enter dash help here, we see that we can specify
a couple of options here
though we don't have that many
and these are advanced options, which we can ignore here
and then your volume.
So you can, for example, use docker volume create
to create a volume named feedback files.
And if you do that and you then run docker volume LS
thereafter, you see that volume as well.
And if you would now run your container
you could also use this new feedback dash files volume here
as a name for the named volume, instead of feedback.
Now, if you don't create the volume in advance,
Docker will create it for you,
but you can also create it manually
if you would like to do that.
Often, of course, you'll leave it up to Dockerfile
because since it creates automatically
if you don't have a strong reason for doing it manually,
why would you do it?
Nonetheless, it is worth knowing that you can control this
with the docker volume command.
Now with this command, you can actually do more.
You can also remove a volume
or remove all unused local volumes
and you can also inspect the volume.
Now let's inspect first.
So first of all, I'll list my volumes.
And then I'll take this feedback volume here
and run docker volume inspect feedback
to look into this volume.
And now we see some information about this volume here.
We see when it was created, we see which drivers used
which we can ignore here.
And we see its name, possible options that are set up,
nothing fancy here.
And then also the mountpoint.
And that's actually the path on our host machine
where this is stored.
I mentioned that Docker creates this automatically for us.
Well, here is where it created this.
Nonetheless, this is not a path
you will find on your system because
this is actually inside of a little virtual machine
Docker set up on your system, so to say.
If there is a basic virtual machine
in which Docker runs, you could say
and this is the path in that machine.
It's not an actual path in your file system
on your host machine.
There, this virtual machine is then again mapped differently
and that's what I meant with,
it's hard to find out where this is actually stored.
Therefore, of course, if you just inspect a volume
to find out where it's stored,
you're not going to be too happy.
But if you, for example, wanna see if it's read only
then you could inspect this because you would see this here
under options, if it would be a read only volume.
Which this one of course isn't
which is why we don't see anything related to that here.
Now, I said we can also remove volumes.
And for example, we can remove the feedback files volume
I created a couple of seconds ago
with docker volume RM, and then the volume name.
If I try to remove feedback though
I get an error because this volume is currently in use.
Of course, we got an active container based on that volume.
So we should first stop feedback dash app, this container
and there after, we will be able to remove this volume.
Though of course you should keep in mind that with that
we're going to remove all these text files
that were created with our app.
So we're removing all that data which we wanted to save
across container restarts.
So that is something you should be aware of.
When you remove a volume, all the data in it is lost.
And if you then create the same volume again,
so a volume with the same name,
that will not bring back your data of course.
Still I'm going to do it here
so did I can show this to you,
but now if I restart my container
with this docker run command, you will note
that if I go there and I try to view feedback.awesome.txt
we fail to get to this file because
I removed the volume in the mean time.
Now we can also use docker volume prune
to remove all unused volumes
in case you had multiple volumes
you wanna get rid off.
Now, I'll again stop my feedback app container here.
And then I'm going to list all my volumes again.
And we only see the newly created feedback app
which was created because I restarted the container
after I last deleted it.
What you also see is
that the anonymous volumes are missing here
because as I explained earlier
anonymous volumes are automatically removed
if the container for which they were created is removed.
And this container is removed when it's stopped
because of dash dash RM option.
Well, and that finally is it about volumes
and what you should know about volumes up to this point.
I know it's a lot, the most important thing
is that you remember
how they work and why you use them.
And that you will remember how they can override each other
so to say.